-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy path_psutil_sunos.c
1787 lines (1569 loc) · 50.2 KB
/
_psutil_sunos.c
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Functions specific to Sun OS Solaris platforms.
*
* Thanks to Justin Venus who originally wrote a consistent part of
* this in Cython which I later on translated in C.
*/
/* fix compilation issue on SunOS 5.10, see:
* https://github.com/giampaolo/psutil/issues/421
* https://github.com/giampaolo/psutil/issues/1077
*/
#define _STRUCTURED_PROC 1
#include <Python.h>
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
# undef _FILE_OFFSET_BITS
# undef _LARGEFILE64_SOURCE
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/proc.h>
#include <sys/swap.h>
#include <sys/sysinfo.h>
#include <sys/mntent.h> // for MNTTAB
#include <sys/mnttab.h>
#include <sys/procfs.h>
#include <sys/sockio.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <utmpx.h>
#include <kstat.h>
#include <sys/ioctl.h>
#include <sys/tihdr.h>
#include <stropts.h>
#include <inet/tcp.h>
#ifndef NEW_MIB_COMPLIANT
/*
* Solaris introduced NEW_MIB_COMPLIANT macro with Update 4.
* See https://github.com/giampaolo/psutil/issues/421
* Prior to Update 4, one has to include mib2 by hand.
*/
#include <inet/mib2.h>
#endif
#include <arpa/inet.h>
#include <net/if.h>
#include <math.h> // fabs()
#include "_psutil_common.h"
#include "_psutil_posix.h"
#include "arch/solaris/environ.h"
#define PSUTIL_TV2DOUBLE(t) (((t).tv_nsec * 0.000000001) + (t).tv_sec)
/*
* Read a file content and fills a C structure with it.
*/
static int
psutil_file_to_struct(char *path, void *fstruct, size_t size) {
int fd;
ssize_t nbytes;
fd = open(path, O_RDONLY);
if (fd == -1) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
return 0;
}
nbytes = read(fd, fstruct, size);
if (nbytes == -1) {
close(fd);
PyErr_SetFromErrno(PyExc_OSError);
return 0;
}
if (nbytes != (ssize_t) size) {
close(fd);
PyErr_SetString(
PyExc_RuntimeError, "read() file structure size mismatch");
return 0;
}
close(fd);
return nbytes;
}
/*
* Return process ppid, rss, vms, ctime, nice, nthreads, status and tty
* as a Python tuple.
*/
static PyObject *
psutil_proc_basic_info(PyObject *self, PyObject *args) {
int pid;
char path[1000];
psinfo_t info;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/psinfo", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
return Py_BuildValue(
"ikkdiiikiiii",
info.pr_ppid, // parent pid
info.pr_rssize, // rss
info.pr_size, // vms
PSUTIL_TV2DOUBLE(info.pr_start), // create time
info.pr_lwp.pr_nice, // nice
info.pr_nlwp, // no. of threads
info.pr_lwp.pr_state, // status code
info.pr_ttydev, // tty nr
(int)info.pr_uid, // real user id
(int)info.pr_euid, // effective user id
(int)info.pr_gid, // real group id
(int)info.pr_egid // effective group id
);
}
/*
* Join array of C strings to C string with delemiter dm.
* Omit empty records.
*/
static int
cstrings_array_to_string(char **joined, char ** array, size_t count, char dm) {
int i;
size_t total_length = 0;
size_t item_length = 0;
char *result = NULL;
char *last = NULL;
if (!array || !joined)
return 0;
for (i=0; i<count; i++) {
if (!array[i])
continue;
item_length = strlen(array[i]) + 1;
total_length += item_length;
}
if (!total_length) {
return 0;
}
result = malloc(total_length);
if (!result) {
PyErr_NoMemory();
return -1;
}
result[0] = '\0';
last = result;
for (i=0; i<count; i++) {
if (!array[i])
continue;
item_length = strlen(array[i]);
memcpy(last, array[i], item_length);
last[item_length] = dm;
last += item_length + 1;
}
result[total_length-1] = '\0';
*joined = result;
return total_length;
}
/*
* Return process name and args as a Python tuple.
*/
static PyObject *
psutil_proc_name_and_args(PyObject *self, PyObject *args) {
int pid;
char path[1000];
psinfo_t info;
size_t argc;
int joined;
char **argv;
char *argv_plain;
const char *procfs_path;
PyObject *py_name = NULL;
PyObject *py_args = NULL;
PyObject *py_retlist = NULL;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/psinfo", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
py_name = PyUnicode_DecodeFSDefault(info.pr_fname);
if (!py_name)
goto error;
/* SunOS truncates arguments to length PRARGSZ, very likely args are truncated.
* The only way to retrieve full command line is to parse process memory */
if (info.pr_argc && strlen(info.pr_psargs) == PRARGSZ-1) {
argv = psutil_read_raw_args(info, procfs_path, &argc);
if (argv) {
joined = cstrings_array_to_string(&argv_plain, argv, argc, ' ');
if (joined > 0) {
py_args = PyUnicode_DecodeFSDefault(argv_plain);
free(argv_plain);
} else if (joined < 0) {
goto error;
}
psutil_free_cstrings_array(argv, argc);
}
}
/* If we can't read process memory or can't decode the result
* then return args from /proc. */
if (!py_args) {
PyErr_Clear();
py_args = PyUnicode_DecodeFSDefault(info.pr_psargs);
}
/* Both methods has been failed. */
if (!py_args)
goto error;
py_retlist = Py_BuildValue("OO", py_name, py_args);
if (!py_retlist)
goto error;
Py_DECREF(py_name);
Py_DECREF(py_args);
return py_retlist;
error:
Py_XDECREF(py_name);
Py_XDECREF(py_args);
Py_XDECREF(py_retlist);
return NULL;
}
/*
* Return process environ block.
*/
static PyObject *
psutil_proc_environ(PyObject *self, PyObject *args) {
int pid;
char path[1000];
psinfo_t info;
const char *procfs_path;
char **env = NULL;
ssize_t env_count = -1;
char *dm;
int i = 0;
PyObject *py_envname = NULL;
PyObject *py_envval = NULL;
PyObject *py_retdict = PyDict_New();
if (! py_retdict)
return PyErr_NoMemory();
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/psinfo", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
goto error;
if (! info.pr_envp) {
AccessDenied("/proc/pid/psinfo struct not set");
goto error;
}
env = psutil_read_raw_env(info, procfs_path, &env_count);
if (! env && env_count != 0)
goto error;
for (i=0; i<env_count; i++) {
if (! env[i])
break;
dm = strchr(env[i], '=');
if (! dm)
continue;
*dm = '\0';
py_envname = PyUnicode_DecodeFSDefault(env[i]);
if (! py_envname)
goto error;
py_envval = PyUnicode_DecodeFSDefault(dm+1);
if (! py_envname)
goto error;
if (PyDict_SetItem(py_retdict, py_envname, py_envval) < 0)
goto error;
Py_CLEAR(py_envname);
Py_CLEAR(py_envval);
}
psutil_free_cstrings_array(env, env_count);
return py_retdict;
error:
if (env && env_count >= 0)
psutil_free_cstrings_array(env, env_count);
Py_XDECREF(py_envname);
Py_XDECREF(py_envval);
Py_XDECREF(py_retdict);
return NULL;
}
/*
* Return process user and system CPU times as a Python tuple.
*/
static PyObject *
psutil_proc_cpu_times(PyObject *self, PyObject *args) {
int pid;
char path[1000];
pstatus_t info;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/status", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
// results are more precise than os.times()
return Py_BuildValue(
"(dddd)",
PSUTIL_TV2DOUBLE(info.pr_utime),
PSUTIL_TV2DOUBLE(info.pr_stime),
PSUTIL_TV2DOUBLE(info.pr_cutime),
PSUTIL_TV2DOUBLE(info.pr_cstime)
);
}
/*
* Return what CPU the process is running on.
*/
static PyObject *
psutil_proc_cpu_num(PyObject *self, PyObject *args) {
int fd = NULL;
int pid;
char path[1000];
struct prheader header;
struct lwpsinfo *lwp = NULL;
int nent;
int size;
int proc_num;
ssize_t nbytes;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/lpsinfo", procfs_path, pid);
fd = open(path, O_RDONLY);
if (fd == -1) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
return NULL;
}
// read header
nbytes = pread(fd, &header, sizeof(header), 0);
if (nbytes == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
if (nbytes != sizeof(header)) {
PyErr_SetString(
PyExc_RuntimeError, "read() file structure size mismatch");
goto error;
}
// malloc
nent = header.pr_nent;
size = header.pr_entsize * nent;
lwp = malloc(size);
if (lwp == NULL) {
PyErr_NoMemory();
goto error;
}
// read the rest
nbytes = pread(fd, lwp, size, sizeof(header));
if (nbytes == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
if (nbytes != size) {
PyErr_SetString(
PyExc_RuntimeError, "read() file structure size mismatch");
goto error;
}
// done
proc_num = lwp->pr_onpro;
close(fd);
free(lwp);
return Py_BuildValue("i", proc_num);
error:
if (fd != -1)
close(fd);
free(lwp);
return NULL;
}
/*
* Return process uids/gids as a Python tuple.
*/
static PyObject *
psutil_proc_cred(PyObject *self, PyObject *args) {
int pid;
char path[1000];
prcred_t info;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/cred", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
return Py_BuildValue("iiiiii",
info.pr_ruid, info.pr_euid, info.pr_suid,
info.pr_rgid, info.pr_egid, info.pr_sgid);
}
/*
* Return process voluntary and involuntary context switches as a Python tuple.
*/
static PyObject *
psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) {
int pid;
char path[1000];
prusage_t info;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/usage", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
return Py_BuildValue("kk", info.pr_vctx, info.pr_ictx);
}
/*
* Process IO counters.
*
* Commented out and left here as a reminder. Apparently we cannot
* retrieve process IO stats because:
* - 'pr_ioch' is a sum of chars read and written, with no distinction
* - 'pr_inblk' and 'pr_oublk', which should be the number of bytes
* read and written, hardly increase and according to:
* http://www.brendangregg.com/Solaris/paper_diskubyp1.pdf
* ...they should be meaningless anyway.
*
static PyObject*
proc_io_counters(PyObject* self, PyObject* args) {
int pid;
char path[1000];
prusage_t info;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/usage", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
// On Solaris we only have 'pr_ioch' which accounts for bytes read
// *and* written.
// 'pr_inblk' and 'pr_oublk' should be expressed in blocks of
// 8KB according to:
// http://www.brendangregg.com/Solaris/paper_diskubyp1.pdf (pag. 8)
return Py_BuildValue("kkkk",
info.pr_ioch,
info.pr_ioch,
info.pr_inblk,
info.pr_oublk);
}
*/
/*
* Return information about a given process thread.
*/
static PyObject *
psutil_proc_query_thread(PyObject *self, PyObject *args) {
int pid, tid;
char path[1000];
lwpstatus_t info;
const char *procfs_path;
if (! PyArg_ParseTuple(args, "iis", &pid, &tid, &procfs_path))
return NULL;
sprintf(path, "%s/%i/lwp/%i/lwpstatus", procfs_path, pid, tid);
if (! psutil_file_to_struct(path, (void *)&info, sizeof(info)))
return NULL;
return Py_BuildValue("dd",
PSUTIL_TV2DOUBLE(info.pr_utime),
PSUTIL_TV2DOUBLE(info.pr_stime));
}
/*
* Return information about system virtual memory.
*/
static PyObject *
psutil_swap_mem(PyObject *self, PyObject *args) {
// XXX (arghhh!)
// total/free swap mem: commented out as for some reason I can't
// manage to get the same results shown by "swap -l", despite the
// code below is exactly the same as:
// http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/
// cmd/swap/swap.c
// We're going to parse "swap -l" output from Python (sigh!)
/*
struct swaptable *st;
struct swapent *swapent;
int i;
struct stat64 statbuf;
char *path;
char fullpath[MAXPATHLEN+1];
int num;
if ((num = swapctl(SC_GETNSWP, NULL)) == -1) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
if (num == 0) {
PyErr_SetString(PyExc_RuntimeError, "no swap devices configured");
return NULL;
}
if ((st = malloc(num * sizeof(swapent_t) + sizeof (int))) == NULL) {
PyErr_SetString(PyExc_RuntimeError, "malloc failed");
return NULL;
}
if ((path = malloc(num * MAXPATHLEN)) == NULL) {
PyErr_SetString(PyExc_RuntimeError, "malloc failed");
return NULL;
}
swapent = st->swt_ent;
for (i = 0; i < num; i++, swapent++) {
swapent->ste_path = path;
path += MAXPATHLEN;
}
st->swt_n = num;
if ((num = swapctl(SC_LIST, st)) == -1) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
swapent = st->swt_ent;
long t = 0, f = 0;
for (i = 0; i < num; i++, swapent++) {
int diskblks_per_page =(int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
t += (long)swapent->ste_pages;
f += (long)swapent->ste_free;
}
free(st);
return Py_BuildValue("(kk)", t, f);
*/
kstat_ctl_t *kc;
kstat_t *k;
cpu_stat_t *cpu;
int cpu_count = 0;
int flag = 0;
uint_t sin = 0;
uint_t sout = 0;
kc = kstat_open();
if (kc == NULL)
return PyErr_SetFromErrno(PyExc_OSError);;
k = kc->kc_chain;
while (k != NULL) {
if ((strncmp(k->ks_name, "cpu_stat", 8) == 0) && \
(kstat_read(kc, k, NULL) != -1) )
{
flag = 1;
cpu = (cpu_stat_t *) k->ks_data;
sin += cpu->cpu_vminfo.pgswapin; // num pages swapped in
sout += cpu->cpu_vminfo.pgswapout; // num pages swapped out
}
cpu_count += 1;
k = k->ks_next;
}
kstat_close(kc);
if (!flag) {
PyErr_SetString(PyExc_RuntimeError, "no swap device was found");
return NULL;
}
return Py_BuildValue("(II)", sin, sout);
}
/*
* Return users currently connected on the system.
*/
static PyObject *
psutil_users(PyObject *self, PyObject *args) {
struct utmpx *ut;
PyObject *py_tuple = NULL;
PyObject *py_username = NULL;
PyObject *py_tty = NULL;
PyObject *py_hostname = NULL;
PyObject *py_user_proc = NULL;
PyObject *py_retlist = PyList_New(0);
if (py_retlist == NULL)
return NULL;
setutxent();
while (NULL != (ut = getutxent())) {
if (ut->ut_type == USER_PROCESS)
py_user_proc = Py_True;
else
py_user_proc = Py_False;
py_username = PyUnicode_DecodeFSDefault(ut->ut_user);
if (! py_username)
goto error;
py_tty = PyUnicode_DecodeFSDefault(ut->ut_line);
if (! py_tty)
goto error;
py_hostname = PyUnicode_DecodeFSDefault(ut->ut_host);
if (! py_hostname)
goto error;
py_tuple = Py_BuildValue(
"(OOOfOi)",
py_username, // username
py_tty, // tty
py_hostname, // hostname
(float)ut->ut_tv.tv_sec, // tstamp
py_user_proc, // (bool) user process
ut->ut_pid // process id
);
if (py_tuple == NULL)
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_CLEAR(py_username);
Py_CLEAR(py_tty);
Py_CLEAR(py_hostname);
Py_CLEAR(py_tuple);
}
endutxent();
return py_retlist;
error:
Py_XDECREF(py_username);
Py_XDECREF(py_tty);
Py_XDECREF(py_hostname);
Py_XDECREF(py_tuple);
Py_DECREF(py_retlist);
endutxent();
return NULL;
}
/*
* Return disk mounted partitions as a list of tuples including device,
* mount point and filesystem type.
*/
static PyObject *
psutil_disk_partitions(PyObject *self, PyObject *args) {
FILE *file;
struct mnttab mt;
PyObject *py_dev = NULL;
PyObject *py_mountp = NULL;
PyObject *py_tuple = NULL;
PyObject *py_retlist = PyList_New(0);
if (py_retlist == NULL)
return NULL;
file = fopen(MNTTAB, "rb");
if (file == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
while (getmntent(file, &mt) == 0) {
py_dev = PyUnicode_DecodeFSDefault(mt.mnt_special);
if (! py_dev)
goto error;
py_mountp = PyUnicode_DecodeFSDefault(mt.mnt_mountp);
if (! py_mountp)
goto error;
py_tuple = Py_BuildValue(
"(OOss)",
py_dev, // device
py_mountp, // mount point
mt.mnt_fstype, // fs type
mt.mnt_mntopts); // options
if (py_tuple == NULL)
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_CLEAR(py_dev);
Py_CLEAR(py_mountp);
Py_CLEAR(py_tuple);
}
fclose(file);
return py_retlist;
error:
Py_XDECREF(py_dev);
Py_XDECREF(py_mountp);
Py_XDECREF(py_tuple);
Py_DECREF(py_retlist);
if (file != NULL)
fclose(file);
return NULL;
}
/*
* Return system-wide CPU times.
*/
static PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args) {
kstat_ctl_t *kc;
kstat_t *ksp;
cpu_stat_t cs;
PyObject *py_retlist = PyList_New(0);
PyObject *py_cputime = NULL;
if (py_retlist == NULL)
return NULL;
kc = kstat_open();
if (kc == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
if (kstat_read(kc, ksp, &cs) == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
py_cputime = Py_BuildValue("ffff",
(float)cs.cpu_sysinfo.cpu[CPU_USER],
(float)cs.cpu_sysinfo.cpu[CPU_KERNEL],
(float)cs.cpu_sysinfo.cpu[CPU_IDLE],
(float)cs.cpu_sysinfo.cpu[CPU_WAIT]);
if (py_cputime == NULL)
goto error;
if (PyList_Append(py_retlist, py_cputime))
goto error;
Py_CLEAR(py_cputime);
}
}
kstat_close(kc);
return py_retlist;
error:
Py_XDECREF(py_cputime);
Py_DECREF(py_retlist);
if (kc != NULL)
kstat_close(kc);
return NULL;
}
/*
* Return disk IO statistics.
*/
static PyObject *
psutil_disk_io_counters(PyObject *self, PyObject *args) {
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_io_t kio;
PyObject *py_retdict = PyDict_New();
PyObject *py_disk_info = NULL;
if (py_retdict == NULL)
return NULL;
kc = kstat_open();
if (kc == NULL) {
PyErr_SetFromErrno(PyExc_OSError);;
goto error;
}
ksp = kc->kc_chain;
while (ksp != NULL) {
if (ksp->ks_type == KSTAT_TYPE_IO) {
if (strcmp(ksp->ks_class, "disk") == 0) {
if (kstat_read(kc, ksp, &kio) == -1) {
kstat_close(kc);
return PyErr_SetFromErrno(PyExc_OSError);;
}
py_disk_info = Py_BuildValue(
"(IIKKLL)",
kio.reads,
kio.writes,
kio.nread,
kio.nwritten,
kio.rtime / 1000 / 1000, // from nano to milli secs
kio.wtime / 1000 / 1000 // from nano to milli secs
);
if (!py_disk_info)
goto error;
if (PyDict_SetItemString(py_retdict, ksp->ks_name,
py_disk_info))
goto error;
Py_CLEAR(py_disk_info);
}
}
ksp = ksp->ks_next;
}
kstat_close(kc);
return py_retdict;
error:
Py_XDECREF(py_disk_info);
Py_DECREF(py_retdict);
if (kc != NULL)
kstat_close(kc);
return NULL;
}
/*
* Return process memory mappings.
*/
static PyObject *
psutil_proc_memory_maps(PyObject *self, PyObject *args) {
int pid;
int fd = -1;
char path[1000];
char perms[10];
const char *name;
struct stat st;
pstatus_t status;
prxmap_t *xmap = NULL, *p;
off_t size;
size_t nread;
int nmap;
uintptr_t pr_addr_sz;
uintptr_t stk_base_sz, brk_base_sz;
const char *procfs_path;
PyObject *py_tuple = NULL;
PyObject *py_path = NULL;
PyObject *py_retlist = PyList_New(0);
if (py_retlist == NULL)
return NULL;
if (! PyArg_ParseTuple(args, "is", &pid, &procfs_path))
goto error;
sprintf(path, "%s/%i/status", procfs_path, pid);
if (! psutil_file_to_struct(path, (void *)&status, sizeof(status)))
goto error;
sprintf(path, "%s/%i/xmap", procfs_path, pid);
if (stat(path, &st) == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
size = st.st_size;
fd = open(path, O_RDONLY);
if (fd == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
}
xmap = (prxmap_t *)malloc(size);
if (xmap == NULL) {
PyErr_NoMemory();
goto error;
}
nread = pread(fd, xmap, size, 0);
nmap = nread / sizeof(prxmap_t);
p = xmap;
while (nmap) {
nmap -= 1;
if (p == NULL) {
p += 1;
continue;
}
perms[0] = '\0';
pr_addr_sz = p->pr_vaddr + p->pr_size;
// perms
sprintf(perms, "%c%c%c%c", p->pr_mflags & MA_READ ? 'r' : '-',
p->pr_mflags & MA_WRITE ? 'w' : '-',
p->pr_mflags & MA_EXEC ? 'x' : '-',
p->pr_mflags & MA_SHARED ? 's' : '-');
// name
if (strlen(p->pr_mapname) > 0) {
name = p->pr_mapname;
}
else {
if ((p->pr_mflags & MA_ISM) || (p->pr_mflags & MA_SHM)) {
name = "[shmid]";
}
else {
stk_base_sz = status.pr_stkbase + status.pr_stksize;
brk_base_sz = status.pr_brkbase + status.pr_brksize;
if ((pr_addr_sz > status.pr_stkbase) &&
(p->pr_vaddr < stk_base_sz)) {
name = "[stack]";
}
else if ((p->pr_mflags & MA_ANON) && \
(pr_addr_sz > status.pr_brkbase) && \
(p->pr_vaddr < brk_base_sz)) {
name = "[heap]";
}
else {
name = "[anon]";
}
}
}
py_path = PyUnicode_DecodeFSDefault(name);
if (! py_path)
goto error;
py_tuple = Py_BuildValue(
"kksOkkk",
(unsigned long)p->pr_vaddr,
(unsigned long)pr_addr_sz,
perms,
py_path,
(unsigned long)p->pr_rss * p->pr_pagesize,
(unsigned long)p->pr_anon * p->pr_pagesize,
(unsigned long)p->pr_locked * p->pr_pagesize);
if (!py_tuple)
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_CLEAR(py_path);
Py_CLEAR(py_tuple);
// increment pointer
p += 1;
}
close(fd);
free(xmap);
return py_retlist;
error:
if (fd != -1)
close(fd);
Py_XDECREF(py_tuple);
Py_XDECREF(py_path);
Py_DECREF(py_retlist);
if (xmap != NULL)
free(xmap);
return NULL;
}
/*
* Return a list of tuples for network I/O statistics.
*/
static PyObject *
psutil_net_io_counters(PyObject *self, PyObject *args) {
kstat_ctl_t *kc = NULL;
kstat_t *ksp;
kstat_named_t *rbytes, *wbytes, *rpkts, *wpkts, *ierrs, *oerrs;
int ret;
int sock = -1;
struct lifreq ifr;
PyObject *py_retdict = PyDict_New();