File: cpu_load.py

package info (click to toggle)
python-mitogen 0.3.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,736 kB
  • sloc: python: 24,806; sh: 198; makefile: 74; perl: 19; ansic: 18
file content (36 lines) | stat: -rw-r--r-- 787 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
"""
Put the machine's CPUs under pressure to increase the likelihood of scheduling
weirdness. Useful for exposing otherwise difficult to hit races in the library.
"""

import ctypes
import multiprocessing
import os

import mitogen.core

LIBC = ctypes.CDLL('libc.so.6')
sched_yield = LIBC.sched_yield


def burn():
    while 1:
        a, b, c = os.urandom(3)
        n = int(((ord(a) << 16) |
                 (ord(b) << 8) |
                 (ord(c) << 0)) / 1.6)
        print(n)
        for x in mitogen.core.range(n): pass
        sched_yield()

mul = 1.5
count = int(mul * multiprocessing.cpu_count())
print(count)

procs = [multiprocessing.Process(target=burn)
         for _ in range(count)]

for i, proc in enumerate(procs):
    print([i])
    proc.start()