[9dae191e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Stanislav Kozina
|
---|
| 3 | * Copyright (c) 2010 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <stats.h>
|
---|
| 37 | #include <sysinfo.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <inttypes.h>
|
---|
[38d150e] | 41 | #include <stdlib.h>
|
---|
[9dae191e] | 42 |
|
---|
| 43 | #define SYSINFO_STATS_MAX_PATH 64
|
---|
| 44 |
|
---|
[e1b6742] | 45 | /** Thread states
|
---|
| 46 | *
|
---|
| 47 | */
|
---|
| 48 | static const char *thread_states[] = {
|
---|
| 49 | "Invalid",
|
---|
| 50 | "Running",
|
---|
| 51 | "Sleeping",
|
---|
| 52 | "Ready",
|
---|
| 53 | "Entering",
|
---|
| 54 | "Exiting",
|
---|
| 55 | "Lingering"
|
---|
| 56 | };
|
---|
| 57 |
|
---|
[80bfb601] | 58 | /** Get CPUs statistics
|
---|
| 59 | *
|
---|
| 60 | * @param count Number of records returned.
|
---|
| 61 | *
|
---|
| 62 | * @return Array of stats_cpu_t structures.
|
---|
| 63 | * If non-NULL then it should be eventually freed
|
---|
| 64 | * by free().
|
---|
| 65 | *
|
---|
| 66 | */
|
---|
[c3d4bb45] | 67 | stats_cpu_t *stats_get_cpus(size_t *count)
|
---|
[9dae191e] | 68 | {
|
---|
| 69 | size_t size = 0;
|
---|
| 70 | stats_cpu_t *stats_cpus =
|
---|
| 71 | (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
|
---|
[a35b458] | 72 |
|
---|
[311bc25] | 73 | if ((size % sizeof(stats_cpu_t)) != 0) {
|
---|
| 74 | if (stats_cpus != NULL)
|
---|
| 75 | free(stats_cpus);
|
---|
| 76 | *count = 0;
|
---|
| 77 | return NULL;
|
---|
| 78 | }
|
---|
[a35b458] | 79 |
|
---|
[9dae191e] | 80 | *count = size / sizeof(stats_cpu_t);
|
---|
| 81 | return stats_cpus;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[80bfb601] | 84 | /** Get physical memory statistics
|
---|
| 85 | *
|
---|
| 86 | *
|
---|
| 87 | * @return Pointer to the stats_physmem_t structure.
|
---|
| 88 | * If non-NULL then it should be eventually freed
|
---|
| 89 | * by free().
|
---|
| 90 | *
|
---|
| 91 | */
|
---|
[c3d4bb45] | 92 | stats_physmem_t *stats_get_physmem(void)
|
---|
[9dae191e] | 93 | {
|
---|
| 94 | size_t size = 0;
|
---|
| 95 | stats_physmem_t *stats_physmem =
|
---|
| 96 | (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
|
---|
[a35b458] | 97 |
|
---|
[311bc25] | 98 | if (size != sizeof(stats_physmem_t)) {
|
---|
| 99 | if (stats_physmem != NULL)
|
---|
| 100 | free(stats_physmem);
|
---|
| 101 | return NULL;
|
---|
| 102 | }
|
---|
[a35b458] | 103 |
|
---|
[9dae191e] | 104 | return stats_physmem;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[dec16a2] | 107 | /** Get task statistics
|
---|
[80bfb601] | 108 | *
|
---|
[dec16a2] | 109 | * @param count Number of records returned.
|
---|
[80bfb601] | 110 | *
|
---|
[dec16a2] | 111 | * @return Array of stats_task_t structures.
|
---|
[80bfb601] | 112 | * If non-NULL then it should be eventually freed
|
---|
| 113 | * by free().
|
---|
| 114 | *
|
---|
| 115 | */
|
---|
[dec16a2] | 116 | stats_task_t *stats_get_tasks(size_t *count)
|
---|
[9dae191e] | 117 | {
|
---|
| 118 | size_t size = 0;
|
---|
[dec16a2] | 119 | stats_task_t *stats_tasks =
|
---|
| 120 | (stats_task_t *) sysinfo_get_data("system.tasks", &size);
|
---|
[a35b458] | 121 |
|
---|
[311bc25] | 122 | if ((size % sizeof(stats_task_t)) != 0) {
|
---|
| 123 | if (stats_tasks != NULL)
|
---|
| 124 | free(stats_tasks);
|
---|
| 125 | *count = 0;
|
---|
| 126 | return NULL;
|
---|
| 127 | }
|
---|
[a35b458] | 128 |
|
---|
[dec16a2] | 129 | *count = size / sizeof(stats_task_t);
|
---|
| 130 | return stats_tasks;
|
---|
[9dae191e] | 131 | }
|
---|
| 132 |
|
---|
[80bfb601] | 133 | /** Get single task statistics
|
---|
| 134 | *
|
---|
| 135 | * @param task_id Task ID we are interested in.
|
---|
| 136 | *
|
---|
| 137 | * @return Pointer to the stats_task_t structure.
|
---|
| 138 | * If non-NULL then it should be eventually freed
|
---|
| 139 | * by free().
|
---|
| 140 | *
|
---|
| 141 | */
|
---|
[c3d4bb45] | 142 | stats_task_t *stats_get_task(task_id_t task_id)
|
---|
[9dae191e] | 143 | {
|
---|
| 144 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
| 145 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
|
---|
[a35b458] | 146 |
|
---|
[9dae191e] | 147 | size_t size = 0;
|
---|
| 148 | stats_task_t *stats_task =
|
---|
| 149 | (stats_task_t *) sysinfo_get_data(name, &size);
|
---|
[a35b458] | 150 |
|
---|
[311bc25] | 151 | if (size != sizeof(stats_task_t)) {
|
---|
| 152 | if (stats_task != NULL)
|
---|
| 153 | free(stats_task);
|
---|
| 154 | return NULL;
|
---|
| 155 | }
|
---|
[a35b458] | 156 |
|
---|
[9dae191e] | 157 | return stats_task;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[dec16a2] | 160 | /** Get thread statistics.
|
---|
[e1b6742] | 161 | *
|
---|
[dec16a2] | 162 | * @param count Number of records returned.
|
---|
[e1b6742] | 163 | *
|
---|
[dec16a2] | 164 | * @return Array of stats_thread_t structures.
|
---|
[e1b6742] | 165 | * If non-NULL then it should be eventually freed
|
---|
| 166 | * by free().
|
---|
| 167 | *
|
---|
| 168 | */
|
---|
[dec16a2] | 169 | stats_thread_t *stats_get_threads(size_t *count)
|
---|
[e1b6742] | 170 | {
|
---|
| 171 | size_t size = 0;
|
---|
[dec16a2] | 172 | stats_thread_t *stats_threads =
|
---|
| 173 | (stats_thread_t *) sysinfo_get_data("system.threads", &size);
|
---|
[a35b458] | 174 |
|
---|
[311bc25] | 175 | if ((size % sizeof(stats_thread_t)) != 0) {
|
---|
| 176 | if (stats_threads != NULL)
|
---|
| 177 | free(stats_threads);
|
---|
| 178 | *count = 0;
|
---|
| 179 | return NULL;
|
---|
| 180 | }
|
---|
[a35b458] | 181 |
|
---|
[dec16a2] | 182 | *count = size / sizeof(stats_thread_t);
|
---|
| 183 | return stats_threads;
|
---|
[e1b6742] | 184 | }
|
---|
| 185 |
|
---|
| 186 | /** Get single thread statistics
|
---|
| 187 | *
|
---|
| 188 | * @param thread_id Thread ID we are interested in.
|
---|
| 189 | *
|
---|
| 190 | * @return Pointer to the stats_thread_t structure.
|
---|
| 191 | * If non-NULL then it should be eventually freed
|
---|
| 192 | * by free().
|
---|
| 193 | *
|
---|
| 194 | */
|
---|
| 195 | stats_thread_t *stats_get_thread(thread_id_t thread_id)
|
---|
| 196 | {
|
---|
| 197 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
| 198 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.threads.%" PRIu64, thread_id);
|
---|
[a35b458] | 199 |
|
---|
[e1b6742] | 200 | size_t size = 0;
|
---|
| 201 | stats_thread_t *stats_thread =
|
---|
| 202 | (stats_thread_t *) sysinfo_get_data(name, &size);
|
---|
[a35b458] | 203 |
|
---|
[311bc25] | 204 | if (size != sizeof(stats_thread_t)) {
|
---|
| 205 | if (stats_thread != NULL)
|
---|
| 206 | free(stats_thread);
|
---|
| 207 | return NULL;
|
---|
| 208 | }
|
---|
[a35b458] | 209 |
|
---|
[e1b6742] | 210 | return stats_thread;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[8eec3c8] | 213 | /** Get exception statistics.
|
---|
| 214 | *
|
---|
| 215 | * @param count Number of records returned.
|
---|
| 216 | *
|
---|
| 217 | * @return Array of stats_exc_t structures.
|
---|
| 218 | * If non-NULL then it should be eventually freed
|
---|
| 219 | * by free().
|
---|
| 220 | *
|
---|
| 221 | */
|
---|
| 222 | stats_exc_t *stats_get_exceptions(size_t *count)
|
---|
| 223 | {
|
---|
| 224 | size_t size = 0;
|
---|
| 225 | stats_exc_t *stats_exceptions =
|
---|
| 226 | (stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
|
---|
[a35b458] | 227 |
|
---|
[311bc25] | 228 | if ((size % sizeof(stats_exc_t)) != 0) {
|
---|
| 229 | if (stats_exceptions != NULL)
|
---|
| 230 | free(stats_exceptions);
|
---|
| 231 | *count = 0;
|
---|
| 232 | return NULL;
|
---|
| 233 | }
|
---|
[a35b458] | 234 |
|
---|
[8eec3c8] | 235 | *count = size / sizeof(stats_exc_t);
|
---|
| 236 | return stats_exceptions;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /** Get single exception statistics
|
---|
| 240 | *
|
---|
| 241 | * @param excn Exception number we are interested in.
|
---|
| 242 | *
|
---|
| 243 | * @return Pointer to the stats_exc_t structure.
|
---|
| 244 | * If non-NULL then it should be eventually freed
|
---|
| 245 | * by free().
|
---|
| 246 | *
|
---|
| 247 | */
|
---|
| 248 | stats_exc_t *stats_get_exception(unsigned int excn)
|
---|
| 249 | {
|
---|
| 250 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
[311bc25] | 251 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
|
---|
[a35b458] | 252 |
|
---|
[8eec3c8] | 253 | size_t size = 0;
|
---|
| 254 | stats_exc_t *stats_exception =
|
---|
| 255 | (stats_exc_t *) sysinfo_get_data(name, &size);
|
---|
[a35b458] | 256 |
|
---|
[311bc25] | 257 | if (size != sizeof(stats_exc_t)) {
|
---|
| 258 | if (stats_exception != NULL)
|
---|
| 259 | free(stats_exception);
|
---|
| 260 | return NULL;
|
---|
| 261 | }
|
---|
[a35b458] | 262 |
|
---|
[8eec3c8] | 263 | return stats_exception;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[80bfb601] | 266 | /** Get system load
|
---|
| 267 | *
|
---|
| 268 | * @param count Number of load records returned.
|
---|
| 269 | *
|
---|
| 270 | * @return Array of load records (load_t).
|
---|
| 271 | * If non-NULL then it should be eventually freed
|
---|
| 272 | * by free().
|
---|
| 273 | *
|
---|
| 274 | */
|
---|
[c3d4bb45] | 275 | load_t *stats_get_load(size_t *count)
|
---|
[9dae191e] | 276 | {
|
---|
| 277 | size_t size = 0;
|
---|
| 278 | load_t *load =
|
---|
| 279 | (load_t *) sysinfo_get_data("system.load", &size);
|
---|
[a35b458] | 280 |
|
---|
[311bc25] | 281 | if ((size % sizeof(load_t)) != 0) {
|
---|
| 282 | if (load != NULL)
|
---|
| 283 | free(load);
|
---|
| 284 | *count = 0;
|
---|
| 285 | return NULL;
|
---|
| 286 | }
|
---|
[a35b458] | 287 |
|
---|
[9dae191e] | 288 | *count = size / sizeof(load_t);
|
---|
| 289 | return load;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[80bfb601] | 292 | /** Print load fixed-point value
|
---|
| 293 | *
|
---|
| 294 | * Print the load record fixed-point value in decimal
|
---|
| 295 | * representation on stdout.
|
---|
| 296 | *
|
---|
| 297 | * @param upper Load record.
|
---|
| 298 | * @param dec_length Number of decimal digits to print.
|
---|
| 299 | *
|
---|
| 300 | */
|
---|
[c3d4bb45] | 301 | void stats_print_load_fragment(load_t upper, unsigned int dec_length)
|
---|
[9dae191e] | 302 | {
|
---|
| 303 | /* Print the whole part */
|
---|
[9696b01] | 304 | printf("%u.", upper / LOAD_UNIT);
|
---|
[a35b458] | 305 |
|
---|
[9696b01] | 306 | load_t rest = (upper % LOAD_UNIT) * 10;
|
---|
[a35b458] | 307 |
|
---|
[9dae191e] | 308 | unsigned int i;
|
---|
| 309 | for (i = 0; i < dec_length; i++) {
|
---|
[9696b01] | 310 | printf("%u", rest / LOAD_UNIT);
|
---|
| 311 | rest = (rest % LOAD_UNIT) * 10;
|
---|
[9dae191e] | 312 | }
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[e1b6742] | 315 | const char *thread_get_state(state_t state)
|
---|
| 316 | {
|
---|
| 317 | if (state <= Lingering)
|
---|
| 318 | return thread_states[state];
|
---|
[a35b458] | 319 |
|
---|
[e1b6742] | 320 | return thread_states[Invalid];
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[9dae191e] | 323 | /** @}
|
---|
| 324 | */
|
---|