[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>
|
---|
[311bc25] | 41 | #include <malloc.h>
|
---|
[d4d74dc] | 42 | #include <unistd.h>
|
---|
[9dae191e] | 43 |
|
---|
| 44 | #define SYSINFO_STATS_MAX_PATH 64
|
---|
| 45 |
|
---|
[e1b6742] | 46 | /** Thread states
|
---|
| 47 | *
|
---|
| 48 | */
|
---|
| 49 | static const char *thread_states[] = {
|
---|
| 50 | "Invalid",
|
---|
| 51 | "Running",
|
---|
| 52 | "Sleeping",
|
---|
| 53 | "Ready",
|
---|
| 54 | "Entering",
|
---|
| 55 | "Exiting",
|
---|
| 56 | "Lingering"
|
---|
| 57 | };
|
---|
| 58 |
|
---|
[80bfb601] | 59 | /** Get CPUs statistics
|
---|
| 60 | *
|
---|
| 61 | * @param count Number of records returned.
|
---|
| 62 | *
|
---|
| 63 | * @return Array of stats_cpu_t structures.
|
---|
| 64 | * If non-NULL then it should be eventually freed
|
---|
| 65 | * by free().
|
---|
| 66 | *
|
---|
| 67 | */
|
---|
[c3d4bb45] | 68 | stats_cpu_t *stats_get_cpus(size_t *count)
|
---|
[9dae191e] | 69 | {
|
---|
| 70 | size_t size = 0;
|
---|
| 71 | stats_cpu_t *stats_cpus =
|
---|
| 72 | (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
|
---|
| 73 |
|
---|
[311bc25] | 74 | if ((size % sizeof(stats_cpu_t)) != 0) {
|
---|
| 75 | if (stats_cpus != NULL)
|
---|
| 76 | free(stats_cpus);
|
---|
| 77 | *count = 0;
|
---|
| 78 | return NULL;
|
---|
| 79 | }
|
---|
[9dae191e] | 80 |
|
---|
| 81 | *count = size / sizeof(stats_cpu_t);
|
---|
| 82 | return stats_cpus;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[80bfb601] | 85 | /** Get physical memory statistics
|
---|
| 86 | *
|
---|
| 87 | *
|
---|
| 88 | * @return Pointer to the stats_physmem_t structure.
|
---|
| 89 | * If non-NULL then it should be eventually freed
|
---|
| 90 | * by free().
|
---|
| 91 | *
|
---|
| 92 | */
|
---|
[c3d4bb45] | 93 | stats_physmem_t *stats_get_physmem(void)
|
---|
[9dae191e] | 94 | {
|
---|
| 95 | size_t size = 0;
|
---|
| 96 | stats_physmem_t *stats_physmem =
|
---|
| 97 | (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
|
---|
| 98 |
|
---|
[311bc25] | 99 | if (size != sizeof(stats_physmem_t)) {
|
---|
| 100 | if (stats_physmem != NULL)
|
---|
| 101 | free(stats_physmem);
|
---|
| 102 | return NULL;
|
---|
| 103 | }
|
---|
[9dae191e] | 104 |
|
---|
| 105 | return stats_physmem;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[dec16a2] | 108 | /** Get task statistics
|
---|
[80bfb601] | 109 | *
|
---|
[dec16a2] | 110 | * @param count Number of records returned.
|
---|
[80bfb601] | 111 | *
|
---|
[dec16a2] | 112 | * @return Array of stats_task_t structures.
|
---|
[80bfb601] | 113 | * If non-NULL then it should be eventually freed
|
---|
| 114 | * by free().
|
---|
| 115 | *
|
---|
| 116 | */
|
---|
[dec16a2] | 117 | stats_task_t *stats_get_tasks(size_t *count)
|
---|
[9dae191e] | 118 | {
|
---|
| 119 | size_t size = 0;
|
---|
[dec16a2] | 120 | stats_task_t *stats_tasks =
|
---|
| 121 | (stats_task_t *) sysinfo_get_data("system.tasks", &size);
|
---|
[9dae191e] | 122 |
|
---|
[311bc25] | 123 | if ((size % sizeof(stats_task_t)) != 0) {
|
---|
| 124 | if (stats_tasks != NULL)
|
---|
| 125 | free(stats_tasks);
|
---|
| 126 | *count = 0;
|
---|
| 127 | return NULL;
|
---|
| 128 | }
|
---|
[9dae191e] | 129 |
|
---|
[dec16a2] | 130 | *count = size / sizeof(stats_task_t);
|
---|
| 131 | return stats_tasks;
|
---|
[9dae191e] | 132 | }
|
---|
| 133 |
|
---|
[80bfb601] | 134 | /** Get single task statistics
|
---|
| 135 | *
|
---|
| 136 | * @param task_id Task ID we are interested in.
|
---|
| 137 | *
|
---|
| 138 | * @return Pointer to the stats_task_t structure.
|
---|
| 139 | * If non-NULL then it should be eventually freed
|
---|
| 140 | * by free().
|
---|
| 141 | *
|
---|
| 142 | */
|
---|
[c3d4bb45] | 143 | stats_task_t *stats_get_task(task_id_t task_id)
|
---|
[9dae191e] | 144 | {
|
---|
| 145 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
| 146 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
|
---|
| 147 |
|
---|
| 148 | size_t size = 0;
|
---|
| 149 | stats_task_t *stats_task =
|
---|
| 150 | (stats_task_t *) sysinfo_get_data(name, &size);
|
---|
| 151 |
|
---|
[311bc25] | 152 | if (size != sizeof(stats_task_t)) {
|
---|
| 153 | if (stats_task != NULL)
|
---|
| 154 | free(stats_task);
|
---|
| 155 | return NULL;
|
---|
| 156 | }
|
---|
[9dae191e] | 157 |
|
---|
| 158 | return stats_task;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[dec16a2] | 161 | /** Get thread statistics.
|
---|
[e1b6742] | 162 | *
|
---|
[dec16a2] | 163 | * @param count Number of records returned.
|
---|
[e1b6742] | 164 | *
|
---|
[dec16a2] | 165 | * @return Array of stats_thread_t structures.
|
---|
[e1b6742] | 166 | * If non-NULL then it should be eventually freed
|
---|
| 167 | * by free().
|
---|
| 168 | *
|
---|
| 169 | */
|
---|
[dec16a2] | 170 | stats_thread_t *stats_get_threads(size_t *count)
|
---|
[e1b6742] | 171 | {
|
---|
| 172 | size_t size = 0;
|
---|
[dec16a2] | 173 | stats_thread_t *stats_threads =
|
---|
| 174 | (stats_thread_t *) sysinfo_get_data("system.threads", &size);
|
---|
[e1b6742] | 175 |
|
---|
[311bc25] | 176 | if ((size % sizeof(stats_thread_t)) != 0) {
|
---|
| 177 | if (stats_threads != NULL)
|
---|
| 178 | free(stats_threads);
|
---|
| 179 | *count = 0;
|
---|
| 180 | return NULL;
|
---|
| 181 | }
|
---|
[e1b6742] | 182 |
|
---|
[dec16a2] | 183 | *count = size / sizeof(stats_thread_t);
|
---|
| 184 | return stats_threads;
|
---|
[e1b6742] | 185 | }
|
---|
| 186 |
|
---|
| 187 | /** Get single thread statistics
|
---|
| 188 | *
|
---|
| 189 | * @param thread_id Thread ID we are interested in.
|
---|
| 190 | *
|
---|
| 191 | * @return Pointer to the stats_thread_t structure.
|
---|
| 192 | * If non-NULL then it should be eventually freed
|
---|
| 193 | * by free().
|
---|
| 194 | *
|
---|
| 195 | */
|
---|
| 196 | stats_thread_t *stats_get_thread(thread_id_t thread_id)
|
---|
| 197 | {
|
---|
| 198 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
| 199 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.threads.%" PRIu64, thread_id);
|
---|
| 200 |
|
---|
| 201 | size_t size = 0;
|
---|
| 202 | stats_thread_t *stats_thread =
|
---|
| 203 | (stats_thread_t *) sysinfo_get_data(name, &size);
|
---|
| 204 |
|
---|
[311bc25] | 205 | if (size != sizeof(stats_thread_t)) {
|
---|
| 206 | if (stats_thread != NULL)
|
---|
| 207 | free(stats_thread);
|
---|
| 208 | return NULL;
|
---|
| 209 | }
|
---|
[e1b6742] | 210 |
|
---|
| 211 | return stats_thread;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[8eec3c8] | 214 | /** Get exception statistics.
|
---|
| 215 | *
|
---|
| 216 | * @param count Number of records returned.
|
---|
| 217 | *
|
---|
| 218 | * @return Array of stats_exc_t structures.
|
---|
| 219 | * If non-NULL then it should be eventually freed
|
---|
| 220 | * by free().
|
---|
| 221 | *
|
---|
| 222 | */
|
---|
| 223 | stats_exc_t *stats_get_exceptions(size_t *count)
|
---|
| 224 | {
|
---|
| 225 | size_t size = 0;
|
---|
| 226 | stats_exc_t *stats_exceptions =
|
---|
| 227 | (stats_exc_t *) sysinfo_get_data("system.exceptions", &size);
|
---|
| 228 |
|
---|
[311bc25] | 229 | if ((size % sizeof(stats_exc_t)) != 0) {
|
---|
| 230 | if (stats_exceptions != NULL)
|
---|
| 231 | free(stats_exceptions);
|
---|
| 232 | *count = 0;
|
---|
| 233 | return NULL;
|
---|
| 234 | }
|
---|
[8eec3c8] | 235 |
|
---|
| 236 | *count = size / sizeof(stats_exc_t);
|
---|
| 237 | return stats_exceptions;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /** Get single exception statistics
|
---|
| 241 | *
|
---|
| 242 | * @param excn Exception number we are interested in.
|
---|
| 243 | *
|
---|
| 244 | * @return Pointer to the stats_exc_t structure.
|
---|
| 245 | * If non-NULL then it should be eventually freed
|
---|
| 246 | * by free().
|
---|
| 247 | *
|
---|
| 248 | */
|
---|
| 249 | stats_exc_t *stats_get_exception(unsigned int excn)
|
---|
| 250 | {
|
---|
| 251 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
[311bc25] | 252 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.exceptions.%u", excn);
|
---|
[8eec3c8] | 253 |
|
---|
| 254 | size_t size = 0;
|
---|
| 255 | stats_exc_t *stats_exception =
|
---|
| 256 | (stats_exc_t *) sysinfo_get_data(name, &size);
|
---|
| 257 |
|
---|
[311bc25] | 258 | if (size != sizeof(stats_exc_t)) {
|
---|
| 259 | if (stats_exception != NULL)
|
---|
| 260 | free(stats_exception);
|
---|
| 261 | return NULL;
|
---|
| 262 | }
|
---|
[8eec3c8] | 263 |
|
---|
| 264 | return stats_exception;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[80bfb601] | 267 | /** Get system load
|
---|
| 268 | *
|
---|
| 269 | * @param count Number of load records returned.
|
---|
| 270 | *
|
---|
| 271 | * @return Array of load records (load_t).
|
---|
| 272 | * If non-NULL then it should be eventually freed
|
---|
| 273 | * by free().
|
---|
| 274 | *
|
---|
| 275 | */
|
---|
[c3d4bb45] | 276 | load_t *stats_get_load(size_t *count)
|
---|
[9dae191e] | 277 | {
|
---|
| 278 | size_t size = 0;
|
---|
| 279 | load_t *load =
|
---|
| 280 | (load_t *) sysinfo_get_data("system.load", &size);
|
---|
| 281 |
|
---|
[311bc25] | 282 | if ((size % sizeof(load_t)) != 0) {
|
---|
| 283 | if (load != NULL)
|
---|
| 284 | free(load);
|
---|
| 285 | *count = 0;
|
---|
| 286 | return NULL;
|
---|
| 287 | }
|
---|
[9dae191e] | 288 |
|
---|
| 289 | *count = size / sizeof(load_t);
|
---|
| 290 | return load;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[80bfb601] | 293 | /** Print load fixed-point value
|
---|
| 294 | *
|
---|
| 295 | * Print the load record fixed-point value in decimal
|
---|
| 296 | * representation on stdout.
|
---|
| 297 | *
|
---|
| 298 | * @param upper Load record.
|
---|
| 299 | * @param dec_length Number of decimal digits to print.
|
---|
| 300 | *
|
---|
| 301 | */
|
---|
[c3d4bb45] | 302 | void stats_print_load_fragment(load_t upper, unsigned int dec_length)
|
---|
[9dae191e] | 303 | {
|
---|
| 304 | /* Print the whole part */
|
---|
[9696b01] | 305 | printf("%u.", upper / LOAD_UNIT);
|
---|
[9dae191e] | 306 |
|
---|
[9696b01] | 307 | load_t rest = (upper % LOAD_UNIT) * 10;
|
---|
[9dae191e] | 308 |
|
---|
| 309 | unsigned int i;
|
---|
| 310 | for (i = 0; i < dec_length; i++) {
|
---|
[9696b01] | 311 | printf("%u", rest / LOAD_UNIT);
|
---|
| 312 | rest = (rest % LOAD_UNIT) * 10;
|
---|
[9dae191e] | 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[e1b6742] | 316 | const char *thread_get_state(state_t state)
|
---|
| 317 | {
|
---|
| 318 | if (state <= Lingering)
|
---|
| 319 | return thread_states[state];
|
---|
| 320 |
|
---|
| 321 | return thread_states[Invalid];
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[9dae191e] | 324 | /** @}
|
---|
| 325 | */
|
---|