root/trunk/libffado/support/tools/listirqinfo.py

Revision 2740, 5.2 kB (checked in by jwoithe, 6 years ago)

[PATCH 04/13] tools: listirqinfo: avoid creating a temporary list.

A function call can also be eliminated as a result.

From Nicolas Boulenguez

Line 
1 #
2 # Copyright (C) 2008 Pieter Palmers
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, version 3 of the License.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 #
16
17 import re
18 import subprocess
19
20 LISTIRQINFO_VERSION="0.3"
21
22 class IRQ:
23     def __init__(self):
24         self.number = None
25         self.scheduling_class = None
26         self.scheduling_priority = None
27         self.process_id = None
28         self.drivers = []
29         self.cpu_counts = []
30     def description (self):
31         s = " IRQ %4s: PID: %5s, count: %18s, Sched %4s (priority %4s), drivers: %s" % \
32             (self.number, self.process_id, self.cpu_counts,
33              self.scheduling_class, self.scheduling_priority,
34              self.drivers)
35         return s
36
37 class SoftIRQ:
38     def __init__(self):
39         self.name = None
40         self.fullname = None
41         self.scheduling_class = None
42         self.scheduling_priority = None
43         self.process_id = None
44         self.cpu_counts = []
45     def description (self):
46         s = " SoftIRQ %12s: PID %6s, Sched %4s (priority %4s), name: %s" % \
47             (self.name, self.process_id ,self.scheduling_class, self.scheduling_priority, self.fullname)
48         return s
49
50 class IRQInfo:
51     def __init__(self):
52         self.softIRQs = {}
53         self.IRQs = {}
54
55     def load(self):
56         # get PID info
57         outtext = subprocess.check_output (('ps', '-eLo', 'pid,cmd,class,rtprio'))
58         rawstr = r"""([0-9]+) +\[IRQ-([0-9]+)\] +([A-Z]{2}) +([-0-9]+)"""
59         compile_obj = re.compile(rawstr)
60         IRQs = {}
61         for line in outtext.splitlines():
62             match_obj = compile_obj.search(line)
63             if match_obj:
64                 irq = IRQ()
65                 irq.process_id = int(match_obj.group(1))
66                 irq.number = int(match_obj.group(2))
67                 irq.scheduling_class = match_obj.group(3)
68                 if match_obj.group(4) != '-':
69                     irq.scheduling_priority = int(match_obj.group(4))
70                 else:
71                     irq.scheduling_priority = None
72                 IRQs[irq.number] = irq
73
74         outtext = subprocess.check_output (('ps', '-eLo', 'pid,cmd,class,rtprio'))
75         rawstr = r"""([0-9]+) +\[softirq-(.*)\] +([A-Z]+) +([-0-9]+)"""
76         compile_obj = re.compile(rawstr)
77         softIRQs = {}
78         for line in outtext.splitlines():
79             match_obj = compile_obj.search(line)
80             if match_obj:
81                 irq = SoftIRQ()
82                 irq.process_id = int(match_obj.group(1))
83                 irq.name = "%s-%s" % (match_obj.group(2),irq.process_id)
84                 irq.fullname = "softirq-%s" % match_obj.group(2)
85                 irq.scheduling_class = match_obj.group(3)
86                 if match_obj.group(4) != '-':
87                     irq.scheduling_priority = int(match_obj.group(4))
88                 else:
89                     irq.scheduling_priority = None
90                 softIRQs[irq.name] = irq
91
92         # get irq info
93         with open ('/proc/interrupts') as f:
94             lines = f.readlines ()
95         nb_cpus = len(lines[0].split())
96         str0 = "([0-9]+): +";
97         str_cpu = "([0-9]+) +"
98         str1="([\w\-]+) +([\w\-, :]+)"
99
100         rawstr = str0;
101         for i in range(nb_cpus):
102             rawstr += str_cpu
103         rawstr += str1
104         compile_obj = re.compile(rawstr)
105
106         for line in lines:
107             match_obj = compile_obj.search(line)
108             if match_obj:
109                 irq_number = int(match_obj.group(1))
110                 if not irq_number in IRQs.keys():
111                     IRQs[irq_number] = IRQ()
112                     IRQs[irq_number].number = irq_number
113
114                 irq = IRQs[irq_number]
115                 cpu_counts = []
116                 for i in range(nb_cpus):
117                     cpu_counts.append(int(match_obj.group(i + 2)))
118                 irq.cpu_counts = cpu_counts
119                 irq.type = match_obj.group(nb_cpus + 2)
120                 drivers = match_obj.group(nb_cpus + 3).split(',')
121                 for driver in drivers:
122                     irq.drivers.append(driver.strip())
123
124                 IRQs[irq.number] = irq
125
126         self.softIRQs = softIRQs
127         self.IRQs = IRQs
128
129     def display (self):
130         print ("Hardware Interrupts:")
131         print ("--------------------")
132         for _, irq in sorted (self.IRQs.items ()):
133             print (irq.description ())
134         print ("")
135         print ("Software Interrupts:")
136         print ("--------------------")
137         for _, irq in sorted (self.softIRQs.items ()):
138             print (irq.description ())
139
140 if __name__== '__main__':
141
142     print( "" )
143     print( "Interrupt list utility " + LISTIRQINFO_VERSION )
144     print( "==========================" )
145     print( "(C) 2008 Pieter Palmers" )
146     print( "" )
147     info = IRQInfo()
148     info.load()
149     info.display()
150     print( "" )
Note: See TracBrowser for help on using the browser.