Skip to content

Commit b19558f

Browse files
committed
Added psutil recipe
1 parent 98e58da commit b19558f

File tree

2 files changed

+418
-0
lines changed

2 files changed

+418
-0
lines changed
Lines changed: 371 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,371 @@
1+
diff -wuprN psutil-0.6.1/psutil/_pslinux.py psutil-0.6.1-new/psutil/_pslinux.py
2+
--- psutil-0.6.1/psutil/_pslinux.py 2012-08-16 17:07:37.000000000 +0200
3+
+++ psutil-0.6.1-new/psutil/_pslinux.py 2013-09-21 14:20:14.240988106 +0200
4+
@@ -455,9 +455,9 @@ class Process(object):
5+
f = open("/proc/%s/io" % self.pid)
6+
try:
7+
for line in f:
8+
- if line.startswith("rchar"):
9+
+ if line.startswith("syscr"):
10+
read_count = int(line.split()[1])
11+
- elif line.startswith("wchar"):
12+
+ elif line.startswith("syscw"):
13+
write_count = int(line.split()[1])
14+
elif line.startswith("read_bytes"):
15+
read_bytes = int(line.split()[1])
16+
@@ -588,7 +588,7 @@ class Process(object):
17+
data['Shared_Clean:'], data['Shared_Clean:'],
18+
data['Private_Clean:'], data['Private_Dirty:'],
19+
data['Referenced:'],
20+
- data['Anonymous:'],
21+
+ data.get('Anonymous:', 0),
22+
data['Swap:'])
23+
f.close()
24+
except EnvironmentError:
25+
diff -wuprN psutil-0.6.1/psutil/_psutil_linux.c psutil-0.6.1-new/psutil/_psutil_linux.c
26+
--- psutil-0.6.1/psutil/_psutil_linux.c 2012-08-13 14:26:21.000000000 +0200
27+
+++ psutil-0.6.1-new/psutil/_psutil_linux.c 2013-09-21 14:15:55.844987989 +0200
28+
@@ -94,6 +94,151 @@ linux_ioprio_set(PyObject* self, PyObjec
29+
}
30+
#endif
31+
32+
+/* Prepare to begin reading and/or writing mount table entries from the
33+
+ beginning of FILE. MODE is as for `fopen'. */
34+
+FILE *setmntent (const char *file, const char *mode)
35+
+{
36+
+ /* Extend the mode parameter with "c" to disable cancellation in the
37+
+ I/O functions and "e" to set FD_CLOEXEC. */
38+
+ size_t modelen = strlen (mode);
39+
+ char newmode[modelen + 3];
40+
+ memcpy (newmode, mode, modelen);
41+
+ memcpy (newmode + modelen, "ce", 3);
42+
+ FILE *result = fopen (file, newmode);
43+
+
44+
+ return result;
45+
+}
46+
+
47+
+/* Close a stream opened with `setmntent'. */
48+
+int endmntent (FILE *stream)
49+
+{
50+
+ if (stream) /* SunOS 4.x allows for NULL stream */
51+
+ fclose (stream);
52+
+ return 1; /* SunOS 4.x says to always return 1 */
53+
+}
54+
+
55+
+/* Since the values in a line are separated by spaces, a name cannot
56+
+ contain a space. Therefore some programs encode spaces in names
57+
+ by the strings "\040". We undo the encoding when reading an entry.
58+
+ The decoding happens in place. */
59+
+static char *
60+
+decode_name (char *buf)
61+
+{
62+
+ char *rp = buf;
63+
+ char *wp = buf;
64+
+
65+
+ do
66+
+ if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
67+
+ {
68+
+ /* \040 is a SPACE. */
69+
+ *wp++ = ' ';
70+
+ rp += 3;
71+
+ }
72+
+ else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
73+
+ {
74+
+ /* \011 is a TAB. */
75+
+ *wp++ = '\t';
76+
+ rp += 3;
77+
+ }
78+
+ else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
79+
+ {
80+
+ /* \012 is a NEWLINE. */
81+
+ *wp++ = '\n';
82+
+ rp += 3;
83+
+ }
84+
+ else if (rp[0] == '\\' && rp[1] == '\\')
85+
+ {
86+
+ /* We have to escape \\ to be able to represent all characters. */
87+
+ *wp++ = '\\';
88+
+ rp += 1;
89+
+ }
90+
+ else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
91+
+ {
92+
+ /* \134 is also \\. */
93+
+ *wp++ = '\\';
94+
+ rp += 3;
95+
+ }
96+
+ else
97+
+ *wp++ = *rp;
98+
+ while (*rp++ != '\0');
99+
+
100+
+ return buf;
101+
+}
102+
+
103+
+/* Read one mount table entry from STREAM. Returns a pointer to storage
104+
+ reused on the next call, or null for EOF or error (use feof/ferror to
105+
+ check). */
106+
+struct mntent *custom_getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
107+
+{
108+
+ char *cp;
109+
+ char *head;
110+
+
111+
+ do
112+
+ {
113+
+ char *end_ptr;
114+
+
115+
+ if (fgets (buffer, bufsiz, stream) == NULL)
116+
+ {
117+
+ return NULL;
118+
+ }
119+
+
120+
+ end_ptr = strchr (buffer, '\n');
121+
+ if (end_ptr != NULL) /* chop newline */
122+
+ *end_ptr = '\0';
123+
+ else
124+
+ {
125+
+ /* Not the whole line was read. Do it now but forget it. */
126+
+ char tmp[1024];
127+
+ while (fgets (tmp, sizeof tmp, stream) != NULL)
128+
+ if (strchr (tmp, '\n') != NULL)
129+
+ break;
130+
+ }
131+
+
132+
+ head = buffer + strspn (buffer, " \t");
133+
+ /* skip empty lines and comment lines: */
134+
+ }
135+
+ while (head[0] == '\0' || head[0] == '#');
136+
+
137+
+ cp = strsep (&head, " \t");
138+
+ mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
139+
+ if (head)
140+
+ head += strspn (head, " \t");
141+
+ cp = strsep (&head, " \t");
142+
+ mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
143+
+ if (head)
144+
+ head += strspn (head, " \t");
145+
+ cp = strsep (&head, " \t");
146+
+ mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
147+
+ if (head)
148+
+ head += strspn (head, " \t");
149+
+ cp = strsep (&head, " \t");
150+
+ mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
151+
+ switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
152+
+ {
153+
+ case 0:
154+
+ mp->mnt_freq = 0;
155+
+ case 1:
156+
+ mp->mnt_passno = 0;
157+
+ case 2:
158+
+ break;
159+
+ }
160+
+
161+
+ return mp;
162+
+}
163+
+
164+
+struct mntent *getmntent_custom (FILE *stream)
165+
+{
166+
+ static struct mntent m;
167+
+ static char *getmntent_buffer;
168+
+
169+
+ #define BUFFER_SIZE 4096
170+
+ if (getmntent_buffer == NULL) {
171+
+ getmntent_buffer = (char *) malloc (BUFFER_SIZE);
172+
+ }
173+
+
174+
+ return custom_getmntent_r (stream, &m, getmntent_buffer, BUFFER_SIZE);
175+
+ #undef BUFFER_SIZE
176+
+}
177+
178+
/*
179+
* Return disk mounted partitions as a list of tuples including device,
180+
@@ -116,9 +261,9 @@ get_disk_partitions(PyObject* self, PyOb
181+
goto error;
182+
}
183+
184+
- while ((entry = getmntent(file))) {
185+
+ while ((entry = getmntent_custom(file))) {
186+
if (entry == NULL) {
187+
- PyErr_Format(PyExc_RuntimeError, "getmntent() failed");
188+
+ PyErr_Format(PyExc_RuntimeError, "getmntent_custom() failed");
189+
goto error;
190+
}
191+
py_tuple = Py_BuildValue("(ssss)", entry->mnt_fsname, // device
192+
@@ -164,6 +309,18 @@ get_sysinfo(PyObject* self, PyObject* ar
193+
}
194+
195+
196+
+static int
197+
+sched_setaffinity(pid_t pid, size_t len, cpu_set_t const * cpusetp)
198+
+{
199+
+ return syscall(__NR_sched_setaffinity, pid, len, cpusetp);
200+
+}
201+
+
202+
+static int
203+
+sched_getaffinity(pid_t pid, size_t len, cpu_set_t const * cpusetp)
204+
+{
205+
+ return syscall(__NR_sched_getaffinity, pid, len, cpusetp);
206+
+}
207+
+
208+
/*
209+
* Return process CPU affinity as a Python long (the bitmask)
210+
*/
211+
diff -wuprN psutil-0.6.1/psutil/_psutil_linux.h psutil-0.6.1-new/psutil/_psutil_linux.h
212+
--- psutil-0.6.1/psutil/_psutil_linux.h 2012-08-13 14:26:21.000000000 +0200
213+
+++ psutil-0.6.1-new/psutil/_psutil_linux.h 2013-09-21 14:15:55.844987989 +0200
214+
@@ -10,6 +10,157 @@
215+
216+
#include <Python.h>
217+
218+
+#ifndef MOUNTED
219+
+#define MOUNTED "/proc/mounts" // android
220+
+#endif
221+
+
222+
+extern int sched_getcpu(void);
223+
+
224+
+/* Our implementation supports up to 32 independent CPUs, which is also
225+
+ * the maximum supported by the kernel at the moment. GLibc uses 1024 by
226+
+ * default.
227+
+ *
228+
+ * If you want to use more than that, you should use CPU_ALLOC() / CPU_FREE()
229+
+ * and the CPU_XXX_S() macro variants.
230+
+ */
231+
+#define CPU_SETSIZE 32
232+
+
233+
+#define __CPU_BITTYPE unsigned long int /* mandated by the kernel */
234+
+#define __CPU_BITSHIFT 5 /* should be log2(BITTYPE) */
235+
+#define __CPU_BITS (1 << __CPU_BITSHIFT)
236+
+#define __CPU_ELT(x) ((x) >> __CPU_BITSHIFT)
237+
+#define __CPU_MASK(x) ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS-1)))
238+
+
239+
+typedef struct {
240+
+ __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ];
241+
+} cpu_set_t;
242+
+
243+
+/* Provide optimized implementation for 32-bit cpu_set_t */
244+
+#if CPU_SETSIZE == __CPU_BITS
245+
+
246+
+# define CPU_ZERO(set_) \
247+
+ do{ \
248+
+ (set_)->__bits[0] = 0; \
249+
+ }while(0)
250+
+
251+
+# define CPU_SET(cpu_,set_) \
252+
+ do {\
253+
+ size_t __cpu = (cpu_); \
254+
+ if (__cpu < CPU_SETSIZE) \
255+
+ (set_)->__bits[0] |= __CPU_MASK(__cpu); \
256+
+ }while (0)
257+
+
258+
+# define CPU_CLR(cpu_,set_) \
259+
+ do {\
260+
+ size_t __cpu = (cpu_); \
261+
+ if (__cpu < CPU_SETSIZE) \
262+
+ (set_)->__bits[0] &= ~__CPU_MASK(__cpu); \
263+
+ }while (0)
264+
+
265+
+# define CPU_ISSET(cpu_, set_) \
266+
+ (__extension__({\
267+
+ size_t __cpu = (cpu_); \
268+
+ (cpu_ < CPU_SETSIZE) \
269+
+ ? ((set_)->__bits[0] & __CPU_MASK(__cpu)) != 0 \
270+
+ : 0; \
271+
+ }))
272+
+
273+
+# define CPU_EQUAL(set1_, set2_) \
274+
+ ((set1_)->__bits[0] == (set2_)->__bits[0])
275+
+
276+
+# define __CPU_OP(dst_, set1_, set2_, op_) \
277+
+ do { \
278+
+ (dst_)->__bits[0] = (set1_)->__bits[0] op_ (set2_)->__bits[0]; \
279+
+ } while (0)
280+
+
281+
+# define CPU_COUNT(set_) __builtin_popcountl((set_)->__bits[0])
282+
+
283+
+#else /* CPU_SETSIZE != __CPU_BITS */
284+
+
285+
+# define CPU_ZERO(set_) CPU_ZERO_S(sizeof(cpu_set_t), set_)
286+
+# define CPU_SET(cpu_,set_) CPU_SET_S(cpu_,sizeof(cpu_set_t),set_)
287+
+# define CPU_CLR(cpu_,set_) CPU_CLR_S(cpu_,sizeof(cpu_set_t),set_)
288+
+# define CPU_ISSET(cpu_,set_) CPU_ISSET_S(cpu_,sizeof(cpu_set_t),set_)
289+
+# define CPU_COUNT(set_) CPU_COUNT_S(sizeof(cpu_set_t),set_)
290+
+# define CPU_EQUAL(set1_,set2_) CPU_EQUAL_S(sizeof(cpu_set_t),set1_,set2_)
291+
+
292+
+# define __CPU_OP(dst_,set1_,set2_,op_) __CPU_OP_S(sizeof(cpu_set_t),dst_,set1_,set2_,op_)
293+
+
294+
+#endif /* CPU_SETSIZE != __CPU_BITS */
295+
+
296+
+#define CPU_AND(set1_,set2_) __CPU_OP(set1_,set2_,&)
297+
+#define CPU_OR(set1_,set2_) __CPU_OP(set1_,set2_,|)
298+
+#define CPU_XOR(set1_,set2_) __CPU_OP(set1_,set2_,^)
299+
+
300+
+/* Support for dynamically-allocated cpu_set_t */
301+
+
302+
+#define CPU_ALLOC_SIZE(count) \
303+
+ __CPU_ELT((count) + (__CPU_BITS-1))*sizeof(__CPU_BITTYPE)
304+
+
305+
+#define CPU_ALLOC(count) __sched_cpualloc((count));
306+
+#define CPU_FREE(set) __sched_cpufree((set))
307+
+
308+
+extern cpu_set_t* __sched_cpualloc(size_t count);
309+
+extern void __sched_cpufree(cpu_set_t* set);
310+
+
311+
+#define CPU_ZERO_S(setsize_,set_) \
312+
+ do { \
313+
+ size_t __nn = 0; \
314+
+ size_t __nn_max = (setsize_)/sizeof(__CPU_BITTYPE); \
315+
+ for (; __nn < __nn_max; __nn++) \
316+
+ (set_)->__bits[__nn] = 0; \
317+
+ } while (0)
318+
+
319+
+#define CPU_SET_S(cpu_,setsize_,set_) \
320+
+ do { \
321+
+ size_t __cpu = (cpu_); \
322+
+ if (__cpu < 8*(setsize_)) \
323+
+ (set_)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
324+
+ } while (0)
325+
+
326+
+#define CPU_CLR_S(cpu_,setsize_,set_) \
327+
+ do { \
328+
+ size_t __cpu = (cpu_); \
329+
+ if (__cpu < 8*(setsize_)) \
330+
+ (set_)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
331+
+ } while (0)
332+
+
333+
+#define CPU_ISSET_S(cpu_, setsize_, set_) \
334+
+ (__extension__ ({ \
335+
+ size_t __cpu = (cpu_); \
336+
+ (__cpu < 8*(setsize_)) \
337+
+ ? ((set_)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
338+
+ : 0; \
339+
+ }))
340+
+
341+
+#define CPU_EQUAL_S(setsize_, set1_, set2_) \
342+
+ (__extension__ ({ \
343+
+ __const __CPU_BITTYPE* __src1 = (set1_)->__bits; \
344+
+ __const __CPU_BITTYPE* __src2 = (set2_)->__bits; \
345+
+ size_t __nn = 0, __nn_max = (setsize_)/sizeof(__CPU_BITTYPE); \
346+
+ for (; __nn < __nn_max; __nn++) { \
347+
+ if (__src1[__nn] != __src2[__nn]) \
348+
+ break; \
349+
+ } \
350+
+ __nn == __nn_max; \
351+
+ }))
352+
+
353+
+#define __CPU_OP_S(setsize_, dstset_, srcset1_, srcset2_, op) \
354+
+ do { \
355+
+ cpu_set_t* __dst = (dstset); \
356+
+ const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
357+
+ const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
358+
+ size_t __nn = 0, __nn_max = (setsize_)/sizeof(__CPU_BITTYPE); \
359+
+ for (; __nn < __nn_max; __nn++) \
360+
+ (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
361+
+ } while (0)
362+
+
363+
+#define CPU_COUNT_S(setsize_, set_) \
364+
+ __sched_cpucount((setsize_), (set_))
365+
+
366+
+extern int __sched_cpucount(size_t setsize, cpu_set_t* set);
367+
+
368+
+
369+
static PyObject* linux_ioprio_get(PyObject* self, PyObject* args);
370+
static PyObject* linux_ioprio_set(PyObject* self, PyObject* args);
371+
static PyObject* get_disk_partitions(PyObject* self, PyObject* args);

0 commit comments

Comments
 (0)