Package elisa :: Package plugins :: Package good :: Package testing :: Module file_descriptor_monitor
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.good.testing.file_descriptor_monitor

 1  # -*- coding: utf-8 -*- 
 2  # Elisa - Home multimedia server 
 3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
 4  # All rights reserved. 
 5  # 
 6  # This file is available under one of two license agreements. 
 7  # 
 8  # This file is licensed under the GPL version 3. 
 9  # See "LICENSE.GPL" in the root of this distribution including a special 
10  # exception to use Elisa with Fluendo's plugins. 
11  # 
12  # The GPL part of Elisa is also available under a commercial licensing 
13  # agreement from Fluendo. 
14  # See "LICENSE.Elisa" in the root directory of this distribution package 
15  # for details on that license. 
16   
17  """ 
18  Filed descriptor load inspector service 
19  """ 
20   
21  __maintainer__ = "Florian Boucault <florian@fluendo.com>" 
22   
23  import os, tempfile 
24   
25  from elisa.core.utils import resources 
26  from elisa.base_components import service_provider 
27   
28  from twisted.internet import reactor 
29   
30 -class FileDescriptorMonitor(service_provider.ServiceProvider):
31 """ 32 Service checking periodically the number of currently opened file 33 descriptors and giving a warning if a certain limit is reached. 34 """ 35 36 default_config = {'max_fd': '70', 37 'frequency': '1.0'} 38 config_doc = {'max_fd': 'Maximum number of file descriptors opened at any' 39 ' time.', 40 'frequency': 'Frequency of the monitoring in times per' 41 ' second'} 42
43 - def initialize(self):
44 service_provider.ServiceProvider.initialize(self) 45 self._delayed = None
46
47 - def start(self):
48 self._max_fd = int(self.config.get('max_fd')) 49 self._frequency = float(self.config.get('frequency')) 50 self.info("Checking %s times per second that no more than %s file \ 51 descriptors are opened", self._frequency, self._max_fd) 52 53 self._delayed = reactor.callLater(1.0/self._frequency, self._check)
54
55 - def stop(self):
56 if self._delayed != None and self._delayed.active(): 57 reactor.cancelCallLater(self._delayed)
58
59 - def _check(self):
60 fds = resources.file_descriptors() 61 if len(fds) > self._max_fd: 62 set_fds = set(fds) 63 count_fds = [] 64 fd_log, path = tempfile.mkstemp(prefix="elisa_", 65 suffix="_opened_fd") 66 67 # if a file descriptor is opened more than 3 times, display it 68 for fd in set_fds: 69 n = fds.count(fd) 70 count_fds.append((fd, n)) 71 if n > 3: 72 self.warning("%s opened %s times" % (fd, n)) 73 74 os.write(fd_log, "Opened file descriptors sorted by frequency\n") 75 os.write(fd_log, "===========================================\n\n") 76 77 # sort from the most opened fd to the least one 78 count_fds.sort(key=lambda e: e[1], reverse=True) 79 80 for fd, n in count_fds: 81 os.write(fd_log, "%s %s\n" % (n, fd)) 82 83 self.warning("Too many file descriptors opened; complete list at " 84 "%s", path) 85 os.close(fd_log) 86 87 self._delayed = reactor.callLater(1.0/self._frequency, self._check)
88