00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00036 #include <stats.h>
00037 #include <sysinfo.h>
00038 #include <errno.h>
00039 #include <stdio.h>
00040 #include <inttypes.h>
00041 #include <malloc.h>
00042
00043 #define SYSINFO_STATS_MAX_PATH 64
00044
00048 static const char *thread_states[] = {
00049 "Invalid",
00050 "Running",
00051 "Sleeping",
00052 "Ready",
00053 "Entering",
00054 "Exiting",
00055 "Lingering"
00056 };
00057
00067 stats_cpu_t *stats_get_cpus(size_t *count)
00068 {
00069 size_t size = 0;
00070 stats_cpu_t *stats_cpus =
00071 (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
00072
00073 if ((size % sizeof(stats_cpu_t)) != 0) {
00074 if (stats_cpus != NULL)
00075 free(stats_cpus);
00076 *count = 0;
00077 return NULL;
00078 }
00079
00080 *count = size / sizeof(stats_cpu_t);
00081 return stats_cpus;
00082 }
00083
00092 stats_physmem_t *stats_get_physmem(void)
00093 {
00094 size_t size = 0;
00095 stats_physmem_t *stats_physmem =
00096 (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
00097
00098 if (size != sizeof(stats_physmem_t)) {
00099 if (stats_physmem != NULL)
00100 free(stats_physmem);
00101 return NULL;
00102 }
00103
00104 return stats_physmem;
00105 }
00106
00116 stats_task_t *stats_get_tasks(size_t *count)
00117 {
00118 size_t size = 0;
00119 stats_task_t *stats_tasks =
00120 (stats_task_t *) sysinfo_get_data("system.tasks", &size);
00121
00122 if ((size % sizeof(stats_task_t)) != 0) {
00123 if (stats_tasks != NULL)
00124 free(stats_tasks);
00125 *count = 0;
00126 return NULL;
00127 }
00128
00129 *count = size / sizeof(stats_task_t);
00130 return stats_tasks;
00131 }
00132
00142 stats_task_t *stats_get_task(task_id_t task_id)
00143 {
00144 char name[SYSINFO_STATS_MAX_PATH];
00145 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
00146
00147 size_t size = 0;
00148 stats_task_t *stats_task =
00149 (stats_task_t *) sysinfo_get_data(name, &size);
00150
00151 if (size != sizeof(stats_task_t)) {
00152 if (stats_task != NULL)
00153 free(stats_task);
00154 return NULL;
00155 }
00156
00157 return stats_task;
00158 }
00159
00169 stats_thread_t *stats_get_threads(size_t *count)
00170 {
00171 size_t size = 0;
00172 stats_thread_t *stats_threads =
00173 (stats_thread_t *) sysinfo_get_data("system.threads", &size);
00174
00175 if ((size % sizeof(stats_thread_t)) != 0) {
00176 if (stats_threads != NULL)
00177 free(stats_threads);
00178 *count = 0;
00179 return NULL;
00180 }
00181
00182 *count = size / sizeof(stats_thread_t);
00183 return stats_threads;
00184 }
00185
00195 stats_thread_t *stats_get_thread(thread_id_t thread_id)
00196 {
00197 char name[SYSINFO_STATS_MAX_PATH];
00198 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.threads.%" PRIu64, thread_id);
00199
00200 size_t size = 0;
00201 stats_thread_t *stats_thread =
00202 (stats_thread_t *) sysinfo_get_data(name, &size);
00203
00204 if (size != sizeof(stats_thread_t)) {
00205 if (stats_thread != NULL)
00206 free(stats_thread);
00207 return NULL;
00208 }
00209
00210 return stats_thread;
00211 }
00212
00222 stats_exc_t *stats_get_exceptions(size_t *count)
00223 {
00224 size_t size = 0;
00225 stats_exc_t *stats_exceptions =
00226 (stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
00227
00228 if ((size % sizeof(stats_exc_t)) != 0) {
00229 if (stats_exceptions != NULL)
00230 free(stats_exceptions);
00231 *count = 0;
00232 return NULL;
00233 }
00234
00235 *count = size / sizeof(stats_exc_t);
00236 return stats_exceptions;
00237 }
00238
00248 stats_exc_t *stats_get_exception(unsigned int excn)
00249 {
00250 char name[SYSINFO_STATS_MAX_PATH];
00251 snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
00252
00253 size_t size = 0;
00254 stats_exc_t *stats_exception =
00255 (stats_exc_t *) sysinfo_get_data(name, &size);
00256
00257 if (size != sizeof(stats_exc_t)) {
00258 if (stats_exception != NULL)
00259 free(stats_exception);
00260 return NULL;
00261 }
00262
00263 return stats_exception;
00264 }
00265
00275 load_t *stats_get_load(size_t *count)
00276 {
00277 size_t size = 0;
00278 load_t *load =
00279 (load_t *) sysinfo_get_data("system.load", &size);
00280
00281 if ((size % sizeof(load_t)) != 0) {
00282 if (load != NULL)
00283 free(load);
00284 *count = 0;
00285 return NULL;
00286 }
00287
00288 *count = size / sizeof(load_t);
00289 return load;
00290 }
00291
00297 sysarg_t stats_get_uptime(void)
00298 {
00299 sysarg_t uptime;
00300 if (sysinfo_get_value("system.uptime", &uptime) != EOK)
00301 uptime = 0;
00302
00303 return uptime;
00304 }
00305
00315 void stats_print_load_fragment(load_t upper, unsigned int dec_length)
00316 {
00317
00318 load_t lower = 65536;
00319
00320
00321 printf("%u.", upper / lower);
00322
00323 load_t rest = (upper % lower) * 10;
00324
00325 unsigned int i;
00326 for (i = 0; i < dec_length; i++) {
00327 printf("%u", rest / lower);
00328 rest = (rest % lower) * 10;
00329 }
00330 }
00331
00332 const char *thread_get_state(state_t state)
00333 {
00334 if (state <= Lingering)
00335 return thread_states[state];
00336
00337 return thread_states[Invalid];
00338 }
00339