HelenOS sources
This source file includes following definitions.
- stats_get_cpus
- stats_get_physmem
- stats_get_tasks
- stats_get_task
- stats_get_threads
- stats_get_ipccs
- stats_get_exceptions
- stats_get_exception
- stats_get_load
- stats_print_load_fragment
- thread_get_state
#include <stats.h>
#include <sysinfo.h>
#include <errno.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#define SYSINFO_STATS_MAX_PATH 64
static const char *thread_states[] = {
"Invalid",
"Running",
"Sleeping",
"Ready",
"Entering",
"Exiting",
"Lingering"
};
stats_cpu_t *stats_get_cpus(size_t *count)
{
size_t size = 0;
stats_cpu_t *stats_cpus =
(stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
if ((size % sizeof(stats_cpu_t)) != 0) {
if (stats_cpus != NULL)
free(stats_cpus);
*count = 0;
return NULL;
}
*count = size / sizeof(stats_cpu_t);
return stats_cpus;
}
stats_physmem_t *stats_get_physmem(void)
{
size_t size = 0;
stats_physmem_t *stats_physmem =
(stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
if (size != sizeof(stats_physmem_t)) {
if (stats_physmem != NULL)
free(stats_physmem);
return NULL;
}
return stats_physmem;
}
stats_task_t *stats_get_tasks(size_t *count)
{
size_t size = 0;
stats_task_t *stats_tasks =
(stats_task_t *) sysinfo_get_data("system.tasks", &size);
if ((size % sizeof(stats_task_t)) != 0) {
if (stats_tasks != NULL)
free(stats_tasks);
*count = 0;
return NULL;
}
*count = size / sizeof(stats_task_t);
return stats_tasks;
}
stats_task_t *stats_get_task(task_id_t task_id)
{
char name[SYSINFO_STATS_MAX_PATH];
snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
size_t size = 0;
stats_task_t *stats_task =
(stats_task_t *) sysinfo_get_data(name, &size);
if (size != sizeof(stats_task_t)) {
if (stats_task != NULL)
free(stats_task);
return NULL;
}
return stats_task;
}
stats_thread_t *stats_get_threads(size_t *count)
{
size_t size = 0;
stats_thread_t *stats_threads =
(stats_thread_t *) sysinfo_get_data("system.threads", &size);
if ((size % sizeof(stats_thread_t)) != 0) {
if (stats_threads != NULL)
free(stats_threads);
*count = 0;
return NULL;
}
*count = size / sizeof(stats_thread_t);
return stats_threads;
}
stats_ipcc_t *stats_get_ipccs(size_t *count)
{
size_t size = 0;
stats_ipcc_t *stats_ipccs =
(stats_ipcc_t *) sysinfo_get_data("system.ipccs", &size);
if ((size % sizeof(stats_ipcc_t)) != 0) {
if (stats_ipccs != NULL)
free(stats_ipccs);
*count = 0;
return NULL;
}
*count = size / sizeof(stats_ipcc_t);
return stats_ipccs;
}
stats_exc_t *stats_get_exceptions(size_t *count)
{
size_t size = 0;
stats_exc_t *stats_exceptions =
(stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
if ((size % sizeof(stats_exc_t)) != 0) {
if (stats_exceptions != NULL)
free(stats_exceptions);
*count = 0;
return NULL;
}
*count = size / sizeof(stats_exc_t);
return stats_exceptions;
}
stats_exc_t *stats_get_exception(unsigned int excn)
{
char name[SYSINFO_STATS_MAX_PATH];
snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
size_t size = 0;
stats_exc_t *stats_exception =
(stats_exc_t *) sysinfo_get_data(name, &size);
if (size != sizeof(stats_exc_t)) {
if (stats_exception != NULL)
free(stats_exception);
return NULL;
}
return stats_exception;
}
load_t *stats_get_load(size_t *count)
{
size_t size = 0;
load_t *load =
(load_t *) sysinfo_get_data("system.load", &size);
if ((size % sizeof(load_t)) != 0) {
if (load != NULL)
free(load);
*count = 0;
return NULL;
}
*count = size / sizeof(load_t);
return load;
}
void stats_print_load_fragment(load_t upper, unsigned int dec_length)
{
printf("%u.", upper / LOAD_UNIT);
load_t rest = (upper % LOAD_UNIT) * 10;
unsigned int i;
for (i = 0; i < dec_length; i++) {
printf("%u", rest / LOAD_UNIT);
rest = (rest % LOAD_UNIT) * 10;
}
}
const char *thread_get_state(state_t state)
{
if (state <= Lingering)
return thread_states[state];
return thread_states[Invalid];
}
HelenOS homepage, sources at GitHub