def open(self): e = "{0} is already open".format(self.name()) assert self.oom_control is None, e assert self.event_oom is None, e assert self.event_pressure is None, e # TODO: CLOEXEC? logger.debug("%s: open", self.name()) self.oom_control = open(self._oom_control_file_path(), "r") self.event_oom = linuxfd.eventfd(initval=0, nonBlocking=True) logger.info("%s: event_oom=%d", self.name(), self.event_oom.fileno()) oom_control_req = "{0} {1}\n".format(self.event_oom.fileno(), self.oom_control.fileno()) with open(self._evt_control_file_path(), "a") as evt_control: evt_control.write(oom_control_req) self.memory_pressure = open(self._memory_pressure_file_path(), "r") self.event_pressure = linuxfd.eventfd(initval=0, nonBlocking=True) logger.info("%s: event_pressure=%d", self.name(), self.event_pressure.fileno()) memory_pressure_req = "{0} {1} critical\n".format( self.event_pressure.fileno(), self.memory_pressure.fileno()) with open(self._evt_control_file_path(), "a") as evt_control: evt_control.write(memory_pressure_req)
def main(pid, memory_usage_factor_limit=MEMORY_USAGE_FACTOR_LIMIT): name = 'MemWatcher' logger.info( f'starting {name} for PID {pid} and memory usage factor limit {memory_usage_factor_limit}' ) efd = linuxfd.eventfd(initval=0, nonBlocking=True) mfd = open(MEMORY_USAGE_IN_BYTES) mfd.fileno() threshold = get_threshold(memory_usage_factor_limit) with open(CGROUP_EVENT_CONTROL, 'w') as f: f.write(f'{efd.fileno()} {mfd.fileno()} {threshold}') epl = select.epoll() epl.register(efd.fileno(), select.EPOLLIN) try: isrunning = True while isrunning: events = epl.poll(-1) for fd, event in events: if fd == efd.fileno() and event & select.EPOLLIN: logger.info('event file received update') logger.info( f'{name} sent signal to the main process with pid {pid}' ) efd.read() os.kill(pid, signal.SIGUSR1) isrunning = False finally: logger.info(f'closing {name}') epl.unregister(efd.fileno()) epl.close()
def open(self): e = "{0} is already open".format(self.name()) assert self.oom_control is None, e assert self.event is None, e # TODO: CLOEXEC? logger.debug("%s: open", self.name()) self.oom_control = open(self._oom_control_file_path(), "r") self.event = linuxfd.eventfd(initval=0, nonBlocking=True) req = "{0} {1}\n".format(self.event_fileno(), self.oom_control.fileno()) with open(self._evt_control_file_path(), "w") as evt_control: evt_control.write(req)
linuxfd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with linuxfd. If not, see <http://www.gnu.org/licenses/>. Written in Python V3.""" import linuxfd, signal, pprint, time, tempfile, os # # test eventfd # efd = linuxfd.eventfd(initval=0, nonBlocking=True) print("\ntesting eventfd (fd={})".format(efd.fileno())) for i in range(0, 3): print(" writing to sempahore") efd.write() try: while True: value = efd.read() print(" read '{}' from semaphore".format(value)) except BlockingIOError: print(" semaphore exhausted") print("\ntesting eventfd (fd={}, mode: counting)".format(efd.fileno())) efd = linuxfd.eventfd(initval=0, semaphore=True, nonBlocking=True) for i in range(0, 3): print(" writing to sempahore")
import sys import linuxfd buffersize = 256 efd=linuxfd.eventfd(0,0) #creates a event file descriptor efd for event notifications cfd=open(sys.argv[1], mode='w') #opens the cgroup.event_control file ofd=open(sys.argv[2], mode='r') #opens the memory.oom_control file buffer = str(efd.fileno())+" "+str(ofd.fileno()) #adds values of file descriptors of event notifications and memory.oom_control file to a buffer cfd.writelines(buffer) #writes the event file descriptor efd and memory.oom_control file desciptor to the cgroups.event_control file desciptor by a buffer cfd.close() #closes the cgroup.event_control file print(ofd.read()) while True: efd.read() print("mem_cgroup oom event received")
Free Software Foundation, either version 3 of the License, or (at your option) any later version. linuxfd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with linuxfd. If not, see <http://www.gnu.org/licenses/>. Written in Python V3.""" import linuxfd, signal, select, time # create special file objects efd = linuxfd.eventfd(initval=0, nonBlocking=True) sfd = linuxfd.signalfd(signalset={signal.SIGINT}, nonBlocking=True) tfd = linuxfd.timerfd(rtc=True, nonBlocking=True) # program timer and mask SIGINT tfd.settime(3, 3) signal.pthread_sigmask(signal.SIG_SETMASK, {signal.SIGINT}) # create epoll instance and register special files epl = select.epoll() epl.register(efd.fileno(), select.EPOLLIN) epl.register(sfd.fileno(), select.EPOLLIN) epl.register(tfd.fileno(), select.EPOLLIN) # start main loop isrunning = True
def get_eventfd(self, close_on_fork=True): fd = linuxfd.eventfd(initval=0, nonBlocking=True) if close_on_fork: self._close_on_fork_fds.add(fd) return fd
linuxfd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with linuxfd. If not, see <http://www.gnu.org/licenses/>. Written in Python V3.""" import linuxfd,signal,pprint,time,tempfile,os # # test eventfd # efd = linuxfd.eventfd(initval=0,nonBlocking=True) print("\ntesting eventfd (fd={})".format(efd.fileno())) for i in range(0,3): print(" writing to sempahore") efd.write() try: while True: value = efd.read() print(" read '{}' from semaphore".format(value)) except BlockingIOError: print(" semaphore exhausted") print("\ntesting eventfd (fd={}, mode: counting)".format(efd.fileno())) efd = linuxfd.eventfd(initval=0,semaphore=True,nonBlocking=True) for i in range(0,3): print(" writing to sempahore")