顯示具有 python3 標籤的文章。 顯示所有文章
顯示具有 python3 標籤的文章。 顯示所有文章

2012年6月13日 星期三

mon process via pid file



#!/usr/bin/python3.2
import os

self_pid_path = '/var/run/monPid.pid'
pid_path_list = ['/var/run/mldonkey.pid', '/var/run/sshd.pid']

# olny a instance run

def check_pid(pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True

pid_file=None

if os.path.exists(self_pid_path):
with open(self_pid_path, 'r') as oldpid_file:
pid = oldpid_file.readline()
if pid.isdigit() and check_pid(int(pid)):
exit()
else:
pid_file = open(self_pid_path, 'w')
else:
pid_file = open(self_pid_path, 'a')

pid_file.write(str(os.getpid()))
pid_file.close()

# olny a instance run

# detect mldonkey alive by pid file

for pid_path in pid_path_list:
pid = 0
program = pid_path[pid_path.rfind('/')+1:-4]
if os.path.exists(pid_path):
with open(pid_path, 'r') as pid_file:
pid = pid_file.readline()[:-1]
if pid.isdigit() and check_pid(int(pid)):
print (program + ': alive')
continue
else:
pid = ''

if pid.isdigit() and check_pid(int(pid)):
print (program + '(' + pid + ')' + 'alive')
else:
print (program + '(' + pid + ')' + 'dead')
os.system('echo `date +"%a %b %d %T"` /etc/init.d/' + program + ' restart >> /var/log/monPidFile')



crontab -e
* * * * * /sbin/monPidFile

2012年4月30日 星期一

七二法則

七二法則是用於計算在某段時間之內成長到原有價值兩倍的方法。

倍數成長年限=72/成長率

真是這麼神奇?


Code:

import math
print (('%5s %5s %5s') % ('%', '72/I', 'Actual Year'))
for i in range(1, 101):
actual = 0
j = 1
k = 1+i/100
while j < 2:
j *=k
actual += 1
print (('%5d %5d %5d') % (i, math.floor(72/i), actual))

Result:

% 72/I Actual Year
1 72 70
2 36 36
3 24 24
4 18 18
5 14 15
6 12 12
7 10 11
8 9 10
9 8 9
10 7 8
11 6 7
12 6 7
13 5 6
14 5 6
15 4 5
16 4 5
17 4 5
18 4 5
19 3 4
20 3 4
21 3 4
22 3 4
23 3 4
24 3 4
25 2 4
26 2 3
27 2 3
28 2 3
29 2 3
30 2 3
31 2 3
32 2 3
33 2 3
34 2 3
35 2 3
36 2 3
37 1 3
38 1 3
39 1 3
40 1 3
41 1 3
42 1 2
43 1 2
44 1 2
45 1 2
46 1 2
47 1 2
48 1 2
49 1 2
50 1 2
51 1 2
52 1 2
53 1 2
54 1 2
55 1 2
56 1 2
57 1 2
58 1 2
59 1 2
60 1 2
61 1 2
62 1 2
63 1 2
64 1 2
65 1 2
66 1 2
67 1 2
68 1 2
69 1 2
70 1 2
71 1 2
72 1 2
73 0 2
74 0 2
75 0 2
76 0 2
77 0 2
78 0 2
79 0 2
80 0 2
81 0 2
82 0 2
83 0 2
84 0 2
85 0 2
86 0 2
87 0 2
88 0 2
89 0 2
90 0 2
91 0 2
92 0 2
93 0 2
94 0 2
95 0 2
96 0 2
97 0 2
98 0 2
99 0 2


結論:
成長率在6或以下就準確,在7到36需要加1

2012年4月18日 星期三

Save power through dynamic frequency

the program split in 2 part:
a program set something and call the script to manage the frequency

/etc/local.d/powerSave.start

#!/bin/bash

#Runtime Power Management
for i in `find /sys/devices/*/power/control`; do echo auto > $i; done;

#USB Autosuspend
for i in `find /sys/bus/usb/devices/*/power/level`; do echo auto > $i; done;
for i in `find /sys/bus/usb/devices/*/power/autosuspend`; do echo 2 > $i; done;

#Use script to mgmt the frequency
cpufreq-set -r -g userspace
echo `date +"%a %b %d %T"` dynamicFrequency start >> /var/log/dynamicFrequency.log
/sbin/dynamicFrequency.py



/etc/local.d/powerSave.stop

#!/bin/bash
kill $(cat /var/run/dynamicFrequency.pid)
cpufreq-set -r -g performance
echo `date +"%a %b %d %T"` dynamicFrequency Stop >> /var/log/dynamicFrequency.log


/sbin/dynamicFrequency.py

#!/usr/bin/python3.2
"""\
This program select a scaling available frequencies based on loadavg
for example the available frequencies:
350000 700000 1050000 1400000 1750000 2100000 2450000 2800000
the current frequencies is 175000
if load >= 80% the frequencies jump 2 step (245000)
if load >= 60% the frequencies jump 1 step (210000)
if load < 20% the frequencies jump -2 step (1050000)
if load < 40% the frequencies jump -1 step (1400000)


"""

import os

# Write the pid into
pid_path = '/var/run/dynamicFrequency.pid'

pidFile=None
if os.path.exists(pid_path):
pidFile = open(pid_path, 'w')
else:
pidFile = open(pid_path, 'a')

pidFile.write(str(os.getpid()))
pidFile.close()

scaling_available_frequencies = None
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies', 'r') as file:
scaling_available_frequencies = file.read().strip().split()

current_frequencies = None
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq', 'r') as file:
raw_current_frequencies = file.read().strip()
if raw_current_frequencies in scaling_available_frequencies:
current_frequencies = scaling_available_frequencies.index(raw_current_frequencies)
else:
current_frequencies = len(scaling_available_frequencies)

from decimal import *
from time import sleep

max = len(scaling_available_frequencies) - 1

while True:
loadavg = None
with open('/proc/loadavg', 'r') as file:
loadavg = Decimal(file.read().strip()[0])

change = 0
if loadavg >= Decimal(0.80):
change = 2
elif loadavg >= Decimal(0.60):
change = 1
elif loadavg < Decimal(0.20):
change = -2
elif loadavg < Decimal(0.40):
change = -1

plan=None
if current_frequencies + change >= max:
plan = max
elif current_frequencies + change < 0:
plan = 0
else:
plan = current_frequencies + change

if plan != current_frequencies:
current_frequencies = plan
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed', 'w') as file:
file.write(scaling_available_frequencies[current_frequencies])
os.system('echo `date +"%a %b %d %T"` dynamicFrequency' + ' loadavg:' + str(loadavg) + ' change frequency to:' + \
scaling_available_frequencies[plan] + ' >> /var/log/dynamicFrequency.log')

sleep(5)

Reference:

2012年4月15日 星期日

Calculate range number either prime number

import math
for n in range(2, 100):
 o = n
 s = set()
 i = 2
 j = math.floor(n)
 while i < j:
  if o % i == 0:
   s.add(i)
   o //= i
  else:
   i+=1
 if len(s):
  print(n, ':' ,','.join(str(v) for v in sorted(s)))
 else:
  print(n, ' is a prime number')


2  is a prime number
3  is a prime number
4 : 2
5  is a prime number
6 : 2,3
7  is a prime number
8 : 2
9 : 3
10 : 2,5
11  is a prime number
12 : 2,3
13  is a prime number
14 : 2,7
15 : 3,5
16 : 2
17  is a prime number
18 : 2,3
19  is a prime number
20 : 2,5
21 : 3,7
22 : 2,11
23  is a prime number
24 : 2,3
25 : 5
26 : 2,13
27 : 3
28 : 2,7
29  is a prime number
30 : 2,3,5
31  is a prime number
32 : 2
33 : 3,11
34 : 2,17
35 : 5,7
36 : 2,3
37  is a prime number
38 : 2,19
39 : 3,13
40 : 2,5
41  is a prime number
42 : 2,3,7
43  is a prime number
44 : 2,11
45 : 3,5
46 : 2,23
47  is a prime number
48 : 2,3
49 : 7
50 : 2,5
51 : 3,17
52 : 2,13
53  is a prime number
54 : 2,3
55 : 5,11
56 : 2,7
57 : 3,19
58 : 2,29
59  is a prime number
60 : 2,3,5
61  is a prime number
62 : 2,31
63 : 3,7
64 : 2
65 : 5,13
66 : 2,3,11
67  is a prime number
68 : 2,17
69 : 3,23
70 : 2,5,7
71  is a prime number
72 : 2,3
73  is a prime number
74 : 2,37
75 : 3,5
76 : 2,19
77 : 7,11
78 : 2,3,13
79  is a prime number
80 : 2,5
81 : 3
82 : 2,41
83  is a prime number
84 : 2,3,7
85 : 5,17
86 : 2,43
87 : 3,29
88 : 2,11
89  is a prime number
90 : 2,3,5
91 : 7,13
92 : 2,23
93 : 3,31
94 : 2,47
95 : 5,19
96 : 2,3
97  is a prime number
98 : 2,7
99 : 3,11