]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
process.py: add support to spawn a multithreaded process
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 25 Jan 2022 15:47:27 +0000 (15:47 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 25 Jan 2022 15:47:56 +0000 (15:47 +0000)
Add support for creating a multithreaded process. The multithreaded
process gets created by spawning a new process, that in turn creates
N threads. The thread function is a while loop, that sleeps for N
seconds. Currently, there is no support for waiting for the threads
to join, and to stop the threads, the spawned process needs to be
terminated/killed.

Suggested-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
ftests/process.py

index 53a2d739518174adcb0c850fccb9e91f1af96e93..41727f5eadf9ce6bf8a90350df8c6ae15c96310c 100644 (file)
@@ -22,6 +22,7 @@
 from cgroup import Cgroup
 from cgroup import CgroupVersion
 import multiprocessing as mp
+import threading as tp
 from run import Run
 from run import RunError
 import time
@@ -38,6 +39,11 @@ class Process(object):
 
         return out_str
 
+    @staticmethod
+    def __thread_infinite_loop(config, sleep_time=1):
+        while 1:
+            time.sleep(sleep_time)
+
     @staticmethod
     def __infinite_loop(config, sleep_time=1):
         cmd = ['/usr/bin/perl', '-e', '\'while(1){{sleep({})}};\''.format(sleep_time)]
@@ -123,6 +129,33 @@ class Process(object):
 
             self.children.append(p)
 
+    def create_threaded_process(self, config, threads_cnt):
+        threads = list()
+
+        for n in range(threads_cnt):
+            sleep_time = n + 1
+            thread = tp.Thread(target=Process.__thread_infinite_loop,
+                               args=(config, sleep_time, ))
+            threads.append(thread)
+
+        for thread in threads:
+            thread.start()
+
+    def create_threaded_process_in_cgroup(self, config, controller, cgname,
+                                          threads=2, cgclassify=True):
+
+        p = mp.Process(target=self.create_threaded_process,
+                       args=(config, threads, ))
+        p.start()
+
+        if cgclassify:
+            Cgroup.classify(config, controller, cgname, p.pid)
+
+        self.children.append(p)
+        self.children_pids.append(p.pid)
+
+        return p.pid
+
     # The caller will block until all children are stopped.
     def join_children(self, config):
         for child in self.children: