hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2013 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <malloc.h>
#include <sched.h>
#include <copperplate/heapobj.h>
#include <copperplate/threadobj.h>
#include <copperplate/clockobj.h>
#include <xenomai/version.h>
#include "sysregfs.h"
#include "../internal.h"
 
#ifdef CONFIG_XENO_PSHARED
 
/*
 * If --enable-pshared was given, we can access the main shared heap
 * to retrieve session-wide information.
 */
 
static char *format_time(ticks_t value, char *buf, size_t bufsz)
{
   unsigned long ms, us, ns;
   char *p = buf;
   ticks_t s;
 
   if (value == 0) {
       strcpy(buf, "-");
       return buf;
   }
 
   s = value / 1000000000ULL;
   ns = value % 1000000000ULL;
   us = ns / 1000;
   ms = us / 1000;
   us %= 1000;
 
   if (s)
       p += snprintf(p, bufsz, "%Lus", s);
 
   if (ms || (s && us))
       p += snprintf(p, bufsz - (p - buf), "%lums", ms);
 
   if (us)
       p += snprintf(p, bufsz - (p - buf), "%luus", us);
 
   return buf;
}
 
int open_threads(struct fsobj *fsobj, void *priv)
{
   struct thread_data *thread_data, *p;
   struct sysgroup_memspec *obj, *tmp;
   char sbuf[64], pbuf[16], tbuf[64];
   struct threadobj_stat statbuf;
   struct fsobstack *o = priv;
   struct threadobj *thobj;
   const char *sched_class;
   int ret, count, len = 0;
 
   ret = heapobj_bind_session(__copperplate_setup_data.session_label);
   if (ret)
       return ret;
 
   fsobstack_init(o);
 
   sysgroup_lock();
   count = sysgroup_count(thread);
   sysgroup_unlock();
 
   if (count == 0)
       goto out;
 
   /*
    * We don't want to hold the sysgroup lock for too long, since
    * it could be contended by a real-time task. So we pull all
    * the per-thread data we need into a local array, before
    * printing out its contents after we dropped the lock.
    */
   thread_data = p = malloc(sizeof(*p) * count);
   if (thread_data == NULL) {
       len = -ENOMEM;
       goto out;
   }
 
   sysgroup_lock();
 
   for_each_sysgroup(obj, tmp, thread) {
       if (p - thread_data >= count)
           break;
       thobj = container_of(obj, struct threadobj, memspec);
       ret = threadobj_lock(thobj);
       if (ret) {
           sysgroup_remove(thread, obj);
           continue;
       }
       namecpy(p->name, thobj->name);
       p->name[sizeof(p->name) - 1] = '\0';
       p->pid = thobj->pid;
       p->priority = threadobj_get_priority(thobj);
       p->policy = threadobj_get_policy(thobj);
       ret = threadobj_stat(thobj, &statbuf);
       threadobj_unlock(thobj);
       if (ret)
           p->cpu = -1;
       else {
           p->status = statbuf.status;
           p->cpu = statbuf.cpu;
           p->timeout = statbuf.timeout;
           p->schedlock = statbuf.schedlock;
       }
       p++;
   }
 
   sysgroup_unlock();
 
   count = p - thread_data;
   if (count == 0)
       goto out_free;
 
   len = fsobstack_grow_format(o, "%-3s  %-6s %-5s  %-8s %-8s  %-10s %s\n",
                   "CPU", "PID", "CLASS", "PRI", "TIMEOUT",
                   "STAT", "NAME");
 
   for (p = thread_data; count > 0; count--) {
       if (kill(p->pid, 0))
           continue;
       snprintf(pbuf, sizeof(pbuf), "%3d", p->priority);
       if (p->cpu < 0) {
           strcpy(tbuf, "????");
           strcpy(sbuf, "??");
       } else {
           format_time(p->timeout, tbuf, sizeof(tbuf));
           format_thread_status(p, sbuf, sizeof(sbuf));
       }
       switch (p->policy) {
       case SCHED_FIFO:
           sched_class = "fifo";
           break;
       case SCHED_RR:
           sched_class = "rr";
           break;
#ifdef SCHED_SPORADIC
       case SCHED_SPORADIC:
           sched_class = "pss";
           break;
#endif
#ifdef SCHED_TP
       case SCHED_TP:
           sched_class = "tp";
           break;
#endif
#ifdef SCHED_QUOTA
       case SCHED_QUOTA:
           sched_class = "quota";
           break;
#endif
#ifdef SCHED_QUOTA
       case SCHED_WEAK:
           sched_class = "weak";
           break;
#endif
       default:
           sched_class = "other";
           break;
       }
       len += fsobstack_grow_format(o,
                        "%3d  %-6d %-5s  %-8s %-8s  %-10s %s\n",
                        p->cpu, p->pid, sched_class, pbuf,
                        tbuf, sbuf, p->name);
       p++;
   }
 
out_free:
   free(thread_data);
out:
   heapobj_unbind_session();
 
   fsobstack_finish(o);
 
   return len < 0 ? len : 0;
}
 
struct heap_data {
   char name[XNOBJECT_NAME_LEN];
   size_t total;
   size_t used;
};
 
int open_heaps(struct fsobj *fsobj, void *priv)
{
   struct sysgroup_memspec *obj, *tmp;
   struct heap_data *heap_data, *p;
   struct shared_heap_memory *heap;
   struct fsobstack *o = priv;
   int ret, count, len = 0;
 
   ret = heapobj_bind_session(__copperplate_setup_data.session_label);
   if (ret)
       return ret;
 
   fsobstack_init(o);
 
   sysgroup_lock();
   count = sysgroup_count(heap);
   sysgroup_unlock();
 
   if (count == 0)
       goto out;
 
   heap_data = p = malloc(sizeof(*p) * count);
   if (heap_data == NULL) {
       len = -ENOMEM;
       goto out;
   }
 
   sysgroup_lock();
 
   /*
    * A heap we find there cannot totally vanish until we drop
    * the group lock, so there is no point in acquiring each heap
    * lock individually for reading the slot.
    */
   for_each_sysgroup(obj, tmp, heap) {
       if (p - heap_data >= count)
           break;
       heap = container_of(obj, struct shared_heap_memory, memspec);
       namecpy(p->name, heap->name);
       p->used = heap->used_size;
       p->total = heap->usable_size;
       p++;
   }
 
   sysgroup_unlock();
 
   count = p - heap_data;
   if (count == 0)
       goto out_free;
 
   len = fsobstack_grow_format(o, "%9s %9s  %s\n",
                   "TOTAL", "USED", "NAME");
 
   for (p = heap_data; count > 0; count--) {
       len += fsobstack_grow_format(o, "%9Zu %9Zu  %s\n",
                        p->total, p->used, p->name);
       p++;
   }
 
out_free:
   free(heap_data);
out:
   heapobj_unbind_session();
 
   fsobstack_finish(o);
 
   return len < 0 ? len : 0;
}
 
#endif /* CONFIG_XENO_PSHARED */
 
int open_version(struct fsobj *fsobj, void *priv)
{
   struct fsobstack *o = priv;
 
   fsobstack_init(o);
   fsobstack_grow_format(o, "%s\n", XENO_VERSION_STRING);
   fsobstack_finish(o);
 
   return 0;
}