Example #1
0
    def test_get_cpu_usage_should_return_the_cpu_usage_since_its_last_invocation(
            self):
        cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test")

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t0"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t0")
        }

        cgroup.initialize_cpu_usage()

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t1"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t1")
        }

        cpu_usage = cgroup.get_cpu_usage()

        self.assertEqual(cpu_usage, 0.031)

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t2"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t2")
        }

        cpu_usage = cgroup.get_cpu_usage()

        self.assertEqual(cpu_usage, 0.045)
Example #2
0
    def test_initialize_cpu_usage_should_set_current_cpu_usage(self):
        cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test")

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t0"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t0")
        }

        cgroup.initialize_cpu_usage()

        self.assertEqual(cgroup._current_cgroup_cpu, 63763)  # pylint: disable=protected-access
        self.assertEqual(cgroup._current_system_cpu, 5496872)  # pylint: disable=protected-access
Example #3
0
    def test_initialize_cpu_usage_should_raise_an_exception_when_called_more_than_once(
            self):
        cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test")

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t0"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t0")
        }

        cgroup.initialize_cpu_usage()

        with self.assertRaises(CGroupsException):
            cgroup.initialize_cpu_usage()
Example #4
0
    def test_get_throttled_time_should_return_the_value_since_its_last_invocation(
            self):
        test_file = os.path.join(self.tmp_dir, "cpu.stat")
        shutil.copyfile(os.path.join(data_dir, "cgroups", "cpu.stat_t0"),
                        test_file)  # throttled_time = 50
        cgroup = CpuCgroup("test", self.tmp_dir)
        cgroup.initialize_cpu_usage()
        shutil.copyfile(os.path.join(data_dir, "cgroups", "cpu.stat_t1"),
                        test_file)  # throttled_time = 2075541442327

        throttled_time = cgroup.get_throttled_time()

        self.assertEqual(throttled_time,
                         float(2075541442327 - 50) / 1E9,
                         "The value of throttled_time is incorrect")
Example #5
0
    def test_initialie_cpu_usage_should_set_the_cgroup_usage_to_0_when_the_cgroup_does_not_exist(
            self):
        cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test")

        io_error_2 = IOError()
        io_error_2.errno = errno.ENOENT  # "No such directory"

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat": os.path.join(data_dir, "cgroups", "proc_stat_t0"),
            os.path.join(cgroup.path, "cpuacct.stat"): io_error_2
        }

        cgroup.initialize_cpu_usage()

        self.assertEqual(cgroup._current_cgroup_cpu, 0)  # pylint: disable=protected-access
        self.assertEqual(cgroup._current_system_cpu, 5496872)  # check the system usage just for test sanity # pylint: disable=protected-access
Example #6
0
    def test_get_cpu_usage_should_return_the_cpu_usage_since_its_last_invocation(
            self):
        osutil = get_osutil()

        cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test")

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t0"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t0")
        }

        cgroup.initialize_cpu_usage()

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t1"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t1")
        }

        cpu_usage = cgroup.get_cpu_usage()

        self.assertEqual(
            cpu_usage,
            round(100.0 * 0.000307697876885 * osutil.get_processor_cores(), 3))

        TestCpuCgroup.mock_read_file_map = {
            "/proc/stat":
            os.path.join(data_dir, "cgroups", "proc_stat_t2"),
            os.path.join(cgroup.path, "cpuacct.stat"):
            os.path.join(data_dir, "cgroups", "cpuacct.stat_t2")
        }

        cpu_usage = cgroup.get_cpu_usage()

        self.assertEqual(
            cpu_usage,
            round(100.0 * 0.000445181085968 * osutil.get_processor_cores(), 3))
Example #7
0
    def test_get_tracked_metrics_should_return_the_throttled_time(self):
        cgroup = CpuCgroup("test", os.path.join(data_dir, "cgroups"))
        cgroup.initialize_cpu_usage()

        def find_throttled_time(metrics):
            return [
                m for m in metrics
                if m.counter == MetricsCounter.THROTTLED_TIME
            ]

        found = find_throttled_time(cgroup.get_tracked_metrics())
        self.assertTrue(
            len(found) == 0,
            "get_tracked_metrics should not fetch the throttled time by default. Found: {0}"
            .format(found))

        found = find_throttled_time(
            cgroup.get_tracked_metrics(track_throttled_time=True))
        self.assertTrue(
            len(found) == 1,
            "get_tracked_metrics should have fetched the throttled time by default. Found: {0}"
            .format(found))