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 <assert.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <inttypes.h>
|
---|
42 |
|
---|
43 | #define SYSINFO_STATS_MAX_PATH 64
|
---|
44 |
|
---|
45 | /** Get CPUs statistics
|
---|
46 | *
|
---|
47 | * @param count Number of records returned.
|
---|
48 | *
|
---|
49 | * @return Array of stats_cpu_t structures.
|
---|
50 | * If non-NULL then it should be eventually freed
|
---|
51 | * by free().
|
---|
52 | *
|
---|
53 | */
|
---|
54 | stats_cpu_t *stats_get_cpus(size_t *count)
|
---|
55 | {
|
---|
56 | size_t size = 0;
|
---|
57 | stats_cpu_t *stats_cpus =
|
---|
58 | (stats_cpu_t *) sysinfo_get_data("system.cpus", &size);
|
---|
59 |
|
---|
60 | assert((size % sizeof(stats_cpu_t)) == 0);
|
---|
61 |
|
---|
62 | *count = size / sizeof(stats_cpu_t);
|
---|
63 | return stats_cpus;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** Get physical memory statistics
|
---|
67 | *
|
---|
68 | *
|
---|
69 | * @return Pointer to the stats_physmem_t structure.
|
---|
70 | * If non-NULL then it should be eventually freed
|
---|
71 | * by free().
|
---|
72 | *
|
---|
73 | */
|
---|
74 | stats_physmem_t *stats_get_physmem(void)
|
---|
75 | {
|
---|
76 | size_t size = 0;
|
---|
77 | stats_physmem_t *stats_physmem =
|
---|
78 | (stats_physmem_t *) sysinfo_get_data("system.physmem", &size);
|
---|
79 |
|
---|
80 | assert((size == sizeof(stats_physmem_t)) || (size == 0));
|
---|
81 |
|
---|
82 | return stats_physmem;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Get task IDs
|
---|
86 | *
|
---|
87 | * @param count Number of IDs returned.
|
---|
88 | *
|
---|
89 | * @return Array of IDs (task_id_t).
|
---|
90 | * If non-NULL then it should be eventually freed
|
---|
91 | * by free().
|
---|
92 | *
|
---|
93 | */
|
---|
94 | task_id_t *stats_get_tasks(size_t *count)
|
---|
95 | {
|
---|
96 | size_t size = 0;
|
---|
97 | task_id_t *ids =
|
---|
98 | (task_id_t *) sysinfo_get_data("system.tasks", &size);
|
---|
99 |
|
---|
100 | assert((size % sizeof(task_id_t)) == 0);
|
---|
101 |
|
---|
102 | *count = size / sizeof(task_id_t);
|
---|
103 | return ids;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Get single task statistics
|
---|
107 | *
|
---|
108 | * @param task_id Task ID we are interested in.
|
---|
109 | *
|
---|
110 | * @return Pointer to the stats_task_t structure.
|
---|
111 | * If non-NULL then it should be eventually freed
|
---|
112 | * by free().
|
---|
113 | *
|
---|
114 | */
|
---|
115 | stats_task_t *stats_get_task(task_id_t task_id)
|
---|
116 | {
|
---|
117 | char name[SYSINFO_STATS_MAX_PATH];
|
---|
118 | snprintf(name, SYSINFO_STATS_MAX_PATH, "system.tasks.%" PRIu64, task_id);
|
---|
119 |
|
---|
120 | size_t size = 0;
|
---|
121 | stats_task_t *stats_task =
|
---|
122 | (stats_task_t *) sysinfo_get_data(name, &size);
|
---|
123 |
|
---|
124 | assert((size == sizeof(stats_task_t)) || (size == 0));
|
---|
125 |
|
---|
126 | return stats_task;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Get system load
|
---|
130 | *
|
---|
131 | * @param count Number of load records returned.
|
---|
132 | *
|
---|
133 | * @return Array of load records (load_t).
|
---|
134 | * If non-NULL then it should be eventually freed
|
---|
135 | * by free().
|
---|
136 | *
|
---|
137 | */
|
---|
138 | load_t *stats_get_load(size_t *count)
|
---|
139 | {
|
---|
140 | size_t size = 0;
|
---|
141 | load_t *load =
|
---|
142 | (load_t *) sysinfo_get_data("system.load", &size);
|
---|
143 |
|
---|
144 | assert((size % sizeof(load_t)) == 0);
|
---|
145 |
|
---|
146 | *count = size / sizeof(load_t);
|
---|
147 | return load;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Get system uptime
|
---|
151 | *
|
---|
152 | * @return System uptime (in seconds).
|
---|
153 | *
|
---|
154 | */
|
---|
155 | sysarg_t stats_get_uptime(void)
|
---|
156 | {
|
---|
157 | sysarg_t uptime;
|
---|
158 | if (sysinfo_get_value("system.uptime", &uptime) != EOK)
|
---|
159 | uptime = 0;
|
---|
160 |
|
---|
161 | return uptime;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Print load fixed-point value
|
---|
165 | *
|
---|
166 | * Print the load record fixed-point value in decimal
|
---|
167 | * representation on stdout.
|
---|
168 | *
|
---|
169 | * @param upper Load record.
|
---|
170 | * @param dec_length Number of decimal digits to print.
|
---|
171 | *
|
---|
172 | */
|
---|
173 | void stats_print_load_fragment(load_t upper, unsigned int dec_length)
|
---|
174 | {
|
---|
175 | /* Magic value from BSD */
|
---|
176 | load_t lower = 65536;
|
---|
177 |
|
---|
178 | /* Print the whole part */
|
---|
179 | printf("%u.", upper / lower);
|
---|
180 |
|
---|
181 | load_t rest = (upper % lower) * 10;
|
---|
182 |
|
---|
183 | unsigned int i;
|
---|
184 | for (i = 0; i < dec_length; i++) {
|
---|
185 | printf("%u", rest / lower);
|
---|
186 | rest = (rest % lower) * 10;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** @}
|
---|
191 | */
|
---|