summaryrefslogtreecommitdiff
path: root/src/libs6ps/s6ps_pfield.c
blob: 6d84ea0f1adc8b5b48312263b1b2d1f829a2603a (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* ISC license. */

#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <sys/sysinfo.h>

#include <skalibs/uint64.h>
#include <skalibs/types.h>
#include <skalibs/strerr.h>
#include <skalibs/tai.h>
#include <skalibs/djbtime.h>
#include <skalibs/stralloc.h>

#include "s6ps.h"

static char const *const fieldheaders[PFIELD_PHAIL] =
{
  "PID",
  "COMM",
  "STAT",
  "PPID",
  "PGRP",
  "SESSION",
  "TTY",
  "TPGID",
  "UTIME",
  "STIME",
  "CUTIME",
  "CSTIME",
  "PRIO",
  "NICE",
  "THREADS",
  "START",
  "VSZ",
  "RSS",
  "RSSLIM",
  "CPU",
  "RTPRIO",
  "RTPOLICY",
  "USER",
  "GROUP",
  "%MEM",
  "WCHAN",
  "COMMAND",
  "ENVIRONMENT",
  "%CPU",
  "TTIME",
  "CTTIME",
  "TSTART",
  "C%CPU"
} ;

char const *const *s6ps_fieldheaders = fieldheaders ;

static char const *const opttable[PFIELD_PHAIL] =
{
  "pid",
  "comm",
  "s",
  "ppid",
  "pgrp",
  "sess",
  "tty",
  "tpgid",
  "utime",
  "stime",
  "cutime",
  "cstime",
  "prio",
  "nice",
  "thcount",
  "start",
  "vsize",
  "rss",
  "rsslimit",
  "psr",
  "rtprio",
  "policy",
  "user",
  "group",
  "pmem",
  "wchan",
  "args",
  "env",
  "pcpu",
  "ttime",
  "cttime",
  "tstart",
  "cpcpu"
} ;

char const *const *s6ps_opttable = opttable ;

static int fmt_64 (pscan_t *p, size_t *pos, size_t *len, uint64_t u)
{                                                          
  if (!stralloc_readyplus(&p->data, UINT64_FMT)) return 0 ;
  *pos = p->data.len ;
  *len = uint64_fmt(p->data.s + *pos, u) ;
  p->data.len += *len ;
  return 1 ;
}                                                          

static int fmt_i (pscan_t *p, size_t *pos, size_t *len, int d)
{
  if (!stralloc_readyplus(&p->data, UINT32_FMT+1)) return 0 ;
  *pos = p->data.len ;
  *len = int_fmt(p->data.s + *pos, d) ;
  p->data.len += *len ;
  return 1 ;
}

static int fmt_pid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->pid) ;
}

static int fmt_comm (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  *pos = p->statlen ;
  *len = p->commlen ;
  return 1 ;
}

static int fmt_s (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  if (!stralloc_readyplus(&p->data, 4)) return 0 ;
  *pos = p->data.len ;
  p->data.s[p->data.len++] = p->data.s[p->state] ;
  if (p->pid == p->session) p->data.s[p->data.len++] = 's' ;
  if (p->threads > 1) p->data.s[p->data.len++] = 'l' ;
  if ((p->tpgid > 0) && ((unsigned int)p->tpgid == p->pgrp))
    p->data.s[p->data.len++] = '+' ;
  if (p->nice) p->data.s[p->data.len++] = (p->nice < 0) ? '<' : 'N' ;
  
  *len = p->data.len - *pos ;
  return 1 ;
}

static int fmt_ppid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->ppid) ;
}

static int fmt_pgrp (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->pgrp) ;
}

static int fmt_session (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->session) ;
}

static int fmt_ttynr(s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  if (p->ttynr)
  {
    size_t tmppos = p->data.len ;
    if (!s6ps_ttycache_lookup(&aux->caches[2], &p->data, p->ttynr)) return 0 ;
    *pos = tmppos ;
    *len = p->data.len - tmppos ;
  }
  else
  {
    if (!stralloc_catb(&p->data, "-", 1)) return 0 ;
    *pos = p->data.len - 1 ;
    *len = 1 ;
  }
  return 1 ;
}

static int fmt_tpgid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return p->tpgid < 0 ? fmt_i(p, pos, len, -1) : fmt_64(p, pos, len, p->tpgid) ;
}

static unsigned int gethz (s6ps_auxinfo_t *aux)
{
  if (!aux->hz)
  {            
    long jiffies = sysconf(_SC_CLK_TCK) ;
    if (jiffies < 1)
    {             
      char fmt[ULONG_FMT + 1] ;
      fmt[long_fmt(fmt, jiffies)] = 0 ;
      strerr_warnw3x("invalid _SC_CLK_TCK value (", fmt, "), using 100") ;
      aux->hz = 100 ;
    }         
    else aux->hz = (unsigned int)jiffies ;
  }
  return aux->hz ;
}                

int s6ps_compute_boottime (s6ps_auxinfo_t *aux, pscan_t *p, unsigned int mypos)
{
  if (!mypos--)
  {
    strerr_warnwu1x("compute boot time - using epoch") ;
    return 0 ;
  }
  else
  {
    unsigned int hz = gethz(aux) ;
    tain offset = { .sec = { .x = p[mypos].start / hz }, .nano = (p[mypos].start % hz) * (1000000000 / hz) } ;
    tain_sub(&aux->boottime, &STAMP, &offset) ;
    return 1 ;
  }
}

static int fmt_jiffies (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len, uint64_t j)
{
  unsigned int hz = gethz(aux) ;
  uint32_t hrs, mins, secs, hfrac ;
  if (!stralloc_readyplus(&p->data, UINT64_FMT + 13)) return 0 ;
  hfrac = (j % hz) * 100 / hz ;
  *pos = p->data.len ;
  j /= hz ;
  secs = j % 60 ; j /= 60 ;
  mins = j % 60 ; j /= 60 ;
  hrs = j % 24 ; j /= 24 ;
  if (j)
  {
    p->data.len += uint64_fmt(p->data.s + p->data.len, j) ;
    p->data.s[p->data.len++] = 'd' ;
  }
  if (j || hrs)
  {
    uint320_fmt(p->data.s + p->data.len, hrs, 2) ;
    p->data.len += 2 ;
    p->data.s[p->data.len++] = 'h' ;
  }
  if (j || hrs || mins)
  {
    uint320_fmt(p->data.s + p->data.len, mins, 2) ;
    p->data.len += 2 ;
    p->data.s[p->data.len++] = 'm' ;
  }
  uint320_fmt(p->data.s + p->data.len, secs, 2) ;
  p->data.len += 2 ;
  if (!j && !hrs && !mins)
  {
    p->data.s[p->data.len++] = '.' ;
    uint320_fmt(p->data.s + p->data.len, hfrac, 2) ;
    p->data.len += 2 ;
  }
  p->data.s[p->data.len++] = 's' ;
  *len = p->data.len - *pos ;
  return 1 ;
}

static int fmt_utime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return fmt_jiffies(aux, p, pos, len, p->utime) ;
}

static int fmt_stime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return fmt_jiffies(aux, p, pos, len, p->stime) ;
}

static int fmt_cutime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return fmt_jiffies(aux, p, pos, len, p->utime + p->cutime) ;
}

static int fmt_cstime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return fmt_jiffies(aux, p, pos, len, p->stime + p->cstime) ;
}

static int fmt_prio (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_i(p, pos, len, p->prio) ;
}

static int fmt_nice (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_i(p, pos, len, p->nice) ;
}

static int fmt_threads (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->threads) ;
}

static int fmt_timedate (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len, struct tm const *tm)
{
  static struct tm nowtm = { .tm_year = 0 } ;
  size_t tmplen ;
  char *tmpstrf = "%F" ;
  if (!nowtm.tm_year && !localtm_from_tai(&nowtm, tain_secp(&STAMP), 1)) return 0 ;
  if (!stralloc_readyplus(&p->data, 20)) return 0 ;
  if (tm->tm_year == nowtm.tm_year && tm->tm_yday == nowtm.tm_yday)
    tmpstrf = "%T" ;
  else if (tm->tm_year == nowtm.tm_year || (tm->tm_year+1 == nowtm.tm_year && (nowtm.tm_mon + 12 - tm->tm_mon) % 12 < 9))
    tmpstrf = "%b%d %R" ;
  tmplen = strftime(p->data.s + p->data.len, 20, tmpstrf, tm) ;
  if (!tmplen) return 0 ;
  *len = tmplen ;
  *pos = p->data.len ;
  p->data.len += tmplen ;
  return 1 ;
}

static int fmt_start (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  struct tm starttm ;
  unsigned int hz = gethz(aux) ;
  tain blah = { .sec = { .x = p->start / hz }, .nano = (p->start % hz) * (1000000000 / hz) } ;
  tain_add(&blah, &aux->boottime, &blah) ;
  if (!localtm_from_tai(&starttm, tain_secp(&blah), 1)) return 0 ;
  return fmt_timedate(aux, p, pos, len, &starttm) ;
}

static unsigned int getpgsz (s6ps_auxinfo_t *aux)
{
  if (!aux->pgsz)
  {
    long sz = sysconf(_SC_PAGESIZE) ;
    if (sz < 1)
    {
      char fmt[ULONG_FMT + 1] ;
      fmt[long_fmt(fmt, sz)] = 0 ;
      strerr_warnw3x("invalid _SC_PAGESIZE value (", fmt, "), using 4096") ;
      aux->pgsz = 4096 ;
    }
    else aux->pgsz = sz ;
  }
  return aux->pgsz ;
}

static int fmt_vsize (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->vsize / 1024) ;
}

static int fmt_rss (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return fmt_64(p, pos, len, p->rss * (getpgsz(aux) / 1024)) ;
}

static int fmt_rsslim (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->rsslim / 1024) ;
}

static int fmt_cpuno (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->cpuno) ;
}

static int fmt_rtprio (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  (void)aux ;
  return fmt_64(p, pos, len, p->rtprio) ;
}

static int fmt_policy (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  static char const *const policies[8] = { "NORMAL", "FIFO", "RR", "BATCH", "ISO", "IDLE", "UNKNOWN", "UNKNOWN" } ;
  size_t tmppos = p->data.len ;
  if (!stralloc_cats(&p->data, policies[p->policy & 7])) return 0 ;
  *pos = tmppos ;
  *len = p->data.len - tmppos ;
  (void)aux ;
  return 1 ;
}

static int fmt_user (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  size_t tmppos = p->data.len ;
  if (!s6ps_pwcache_lookup(&aux->caches[0], &p->data, p->uid)) return 0 ;
  *pos = tmppos ;
  *len = p->data.len - tmppos ;
  return 1 ;
}

static int fmt_group (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  size_t tmppos = p->data.len ;
  if (!s6ps_grcache_lookup(&aux->caches[1], &p->data, p->gid)) return 0 ;
  *pos = tmppos ;
  *len = p->data.len - tmppos ;
  return 1 ;
}

static uint64_t gettotalmem (s6ps_auxinfo_t *aux)
{
  if (!aux->totalmem)
  {
    struct sysinfo si = { .totalram = 0, .loads = { 0, 0, 0 } } ;
    if (sysinfo(&si) < 0) return 0 ;
    aux->totalmem = si.totalram ;
    aux->totalmem *= si.mem_unit ;
  }
  return aux->totalmem ;
}

static int percent (stralloc *sa, unsigned int n, size_t *pos, size_t *len)
{
  if (!stralloc_readyplus(sa, UINT64_FMT+1)) return 0 ;
  *pos = sa->len ;
  sa->len += uint_fmt(sa->s + sa->len, n / 100) ;
  sa->s[sa->len++] = '.' ;
  uint0_fmt(sa->s + sa->len, n % 100, 2) ;
  sa->len += 2 ;
  *len = sa->len - *pos ;
  return 1 ;
}

static int fmt_pmem (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  uint64_t l = gettotalmem(aux) ;
  return l ? percent(&p->data, p->rss * getpgsz(aux) * 10000 / l, pos, len) : 0 ;
}

static int fmt_wchan (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  size_t tmppos = p->data.len ;
  if (!s6ps_wchan_lookup(&aux->wchan, &p->data, p->wchan)) return 0 ;
  *len = p->data.len - tmppos ;
  *pos = tmppos ;
  return 1 ;
}

static int fmt_args (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  if (!stralloc_readyplus(&p->data, (p->height << 2) + (p->cmdlen ? p->cmdlen : p->commlen + (p->data.s[p->state] == 'Z' ? 11 : 3))))
    return 0 ;
  *pos = p->data.len ;
  if (p->height)
  {
    unsigned int i = 0 ;
    for (; i < 4 * (unsigned int)p->height - 3 ; i++)
      p->data.s[p->data.len + i] = ' ' ;
    memcpy(p->data.s + p->data.len + 4 * p->height - 3, "\\_ ", 3) ;
    p->data.len += p->height << 2 ;
  }
  if (p->cmdlen)
  {
    char const *r = p->data.s + p->statlen + p->commlen ;
    char *w = p->data.s + p->data.len ;
    size_t i = p->cmdlen ;
    while (i--)
    {
      char c = *r++ ;
      *w++ = c ? c : ' ' ;
    }
    p->data.len += p->cmdlen ;
  }
  else if (p->data.s[p->state] == 'Z')
  {
    stralloc_catb(&p->data, p->data.s + uint32_fmt(0, p->pid) + 2, p->commlen) ;
    stralloc_catb(&p->data, " <defunct>", 10) ;
  }
  else
    stralloc_catb(&p->data, p->data.s + uint32_fmt(0, p->pid) + 1, p->commlen+2) ;
  *len = p->data.len - *pos ;
  (void)aux ;
  return 1 ;
}

static int fmt_env (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  size_t i = 0 ;
  if (!p->envlen)
  {
    if (!stralloc_catb(&p->data, "*", 1)) return 0 ;
    *pos = p->data.len - 1 ;
    *len = 1 ;
    return 1 ;
  }
  *pos = p->statlen + p->commlen + p->cmdlen ;
  *len = p->envlen ;
  for (; i < *len ; i++)
    if (!p->data.s[*pos + i]) p->data.s[*pos + i] = ' ' ;
  (void)aux ;
  return 1 ;
}

static uint64_t gettotalj (s6ps_auxinfo_t *aux, uint64_t j)
{
  tain totaltime ;
  unsigned int hz = gethz(aux) ;
  tain_sub(&totaltime, &STAMP, &aux->boottime) ;
  j = totaltime.sec.x * hz + totaltime.nano / (1000000000 / hz) - j ;
  if (!j) j = 1 ;
  return j ;
}

static int fmt_pcpu (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return percent(&p->data, 10000 * (p->utime + p->stime) / gettotalj(aux, p->start), pos, len) ;
}

static int fmt_ttime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) 
{
  return fmt_jiffies(aux, p, pos, len, p->utime + p->stime) ;
}

static int fmt_cttime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return fmt_jiffies(aux, p, pos, len, p->utime + p->stime + p->cutime + p->cstime) ;
}

static int fmt_tstart (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  unsigned int hz = gethz(aux) ;
  tain blah = { .sec = { .x = p->start / hz }, .nano = (p->start % hz) * (1000000000 / hz) } ;
  if (!stralloc_readyplus(&p->data, TIMESTAMP)) return 0 ;
  tain_add(&blah, &aux->boottime, &blah) ;
  *pos = p->data.len ;
  *len = timestamp_fmt(p->data.s + p->data.len, &blah) ;
  p->data.len += *len ;
  return 1 ;
}

static int fmt_cpcpu (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
  return percent(&p->data, 10000 * (p->utime + p->stime + p->cutime + p->cstime) / gettotalj(aux, p->start), pos, len) ;
}

static pfieldfmt_func_ref const pfieldfmt_table[PFIELD_PHAIL] =
{
  &fmt_pid,
  &fmt_comm,
  &fmt_s,
  &fmt_ppid,
  &fmt_pgrp,
  &fmt_session,
  &fmt_ttynr,
  &fmt_tpgid,
  &fmt_utime,
  &fmt_stime,
  &fmt_cutime,
  &fmt_cstime,
  &fmt_prio,
  &fmt_nice,
  &fmt_threads,
  &fmt_start,
  &fmt_vsize,
  &fmt_rss,
  &fmt_rsslim,
  &fmt_cpuno,
  &fmt_rtprio,
  &fmt_policy,
  &fmt_user,
  &fmt_group,
  &fmt_pmem,
  &fmt_wchan,
  &fmt_args,
  &fmt_env,
  &fmt_pcpu,
  &fmt_ttime,
  &fmt_cttime,
  &fmt_tstart,
  &fmt_cpcpu
} ;

pfieldfmt_func_ref const *const s6ps_pfield_fmt = pfieldfmt_table ;