test_cpu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <linux/limits.h>
  4. #include <sys/param.h>
  5. #include <sys/sysinfo.h>
  6. #include <sys/wait.h>
  7. #include <errno.h>
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include "kselftest.h"
  13. #include "cgroup_util.h"
  14. enum hog_clock_type {
  15. // Count elapsed time using the CLOCK_PROCESS_CPUTIME_ID clock.
  16. CPU_HOG_CLOCK_PROCESS,
  17. // Count elapsed time using system wallclock time.
  18. CPU_HOG_CLOCK_WALL,
  19. };
  20. struct cpu_hogger {
  21. char *cgroup;
  22. pid_t pid;
  23. long usage;
  24. };
  25. struct cpu_hog_func_param {
  26. int nprocs;
  27. struct timespec ts;
  28. enum hog_clock_type clock_type;
  29. };
  30. /*
  31. * This test creates two nested cgroups with and without enabling
  32. * the cpu controller.
  33. */
  34. static int test_cpucg_subtree_control(const char *root)
  35. {
  36. char *parent = NULL, *child = NULL, *parent2 = NULL, *child2 = NULL;
  37. int ret = KSFT_FAIL;
  38. // Create two nested cgroups with the cpu controller enabled.
  39. parent = cg_name(root, "cpucg_test_0");
  40. if (!parent)
  41. goto cleanup;
  42. if (cg_create(parent))
  43. goto cleanup;
  44. if (cg_write(parent, "cgroup.subtree_control", "+cpu"))
  45. goto cleanup;
  46. child = cg_name(parent, "cpucg_test_child");
  47. if (!child)
  48. goto cleanup;
  49. if (cg_create(child))
  50. goto cleanup;
  51. if (cg_read_strstr(child, "cgroup.controllers", "cpu"))
  52. goto cleanup;
  53. // Create two nested cgroups without enabling the cpu controller.
  54. parent2 = cg_name(root, "cpucg_test_1");
  55. if (!parent2)
  56. goto cleanup;
  57. if (cg_create(parent2))
  58. goto cleanup;
  59. child2 = cg_name(parent2, "cpucg_test_child");
  60. if (!child2)
  61. goto cleanup;
  62. if (cg_create(child2))
  63. goto cleanup;
  64. if (!cg_read_strstr(child2, "cgroup.controllers", "cpu"))
  65. goto cleanup;
  66. ret = KSFT_PASS;
  67. cleanup:
  68. cg_destroy(child);
  69. free(child);
  70. cg_destroy(child2);
  71. free(child2);
  72. cg_destroy(parent);
  73. free(parent);
  74. cg_destroy(parent2);
  75. free(parent2);
  76. return ret;
  77. }
  78. static void *hog_cpu_thread_func(void *arg)
  79. {
  80. while (1)
  81. ;
  82. return NULL;
  83. }
  84. static struct timespec
  85. timespec_sub(const struct timespec *lhs, const struct timespec *rhs)
  86. {
  87. struct timespec zero = {
  88. .tv_sec = 0,
  89. .tv_nsec = 0,
  90. };
  91. struct timespec ret;
  92. if (lhs->tv_sec < rhs->tv_sec)
  93. return zero;
  94. ret.tv_sec = lhs->tv_sec - rhs->tv_sec;
  95. if (lhs->tv_nsec < rhs->tv_nsec) {
  96. if (ret.tv_sec == 0)
  97. return zero;
  98. ret.tv_sec--;
  99. ret.tv_nsec = NSEC_PER_SEC - rhs->tv_nsec + lhs->tv_nsec;
  100. } else
  101. ret.tv_nsec = lhs->tv_nsec - rhs->tv_nsec;
  102. return ret;
  103. }
  104. static int hog_cpus_timed(const char *cgroup, void *arg)
  105. {
  106. const struct cpu_hog_func_param *param =
  107. (struct cpu_hog_func_param *)arg;
  108. struct timespec ts_run = param->ts;
  109. struct timespec ts_remaining = ts_run;
  110. struct timespec ts_start;
  111. int i, ret;
  112. ret = clock_gettime(CLOCK_MONOTONIC, &ts_start);
  113. if (ret != 0)
  114. return ret;
  115. for (i = 0; i < param->nprocs; i++) {
  116. pthread_t tid;
  117. ret = pthread_create(&tid, NULL, &hog_cpu_thread_func, NULL);
  118. if (ret != 0)
  119. return ret;
  120. }
  121. while (ts_remaining.tv_sec > 0 || ts_remaining.tv_nsec > 0) {
  122. struct timespec ts_total;
  123. ret = nanosleep(&ts_remaining, NULL);
  124. if (ret && errno != EINTR)
  125. return ret;
  126. if (param->clock_type == CPU_HOG_CLOCK_PROCESS) {
  127. ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_total);
  128. if (ret != 0)
  129. return ret;
  130. } else {
  131. struct timespec ts_current;
  132. ret = clock_gettime(CLOCK_MONOTONIC, &ts_current);
  133. if (ret != 0)
  134. return ret;
  135. ts_total = timespec_sub(&ts_current, &ts_start);
  136. }
  137. ts_remaining = timespec_sub(&ts_run, &ts_total);
  138. }
  139. return 0;
  140. }
  141. /*
  142. * Creates a cpu cgroup, burns a CPU for a few quanta, and verifies that
  143. * cpu.stat shows the expected output.
  144. */
  145. static int test_cpucg_stats(const char *root)
  146. {
  147. int ret = KSFT_FAIL;
  148. long usage_usec, user_usec, system_usec;
  149. long usage_seconds = 2;
  150. long expected_usage_usec = usage_seconds * USEC_PER_SEC;
  151. char *cpucg;
  152. cpucg = cg_name(root, "cpucg_test");
  153. if (!cpucg)
  154. goto cleanup;
  155. if (cg_create(cpucg))
  156. goto cleanup;
  157. usage_usec = cg_read_key_long(cpucg, "cpu.stat", "usage_usec");
  158. user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
  159. system_usec = cg_read_key_long(cpucg, "cpu.stat", "system_usec");
  160. if (usage_usec != 0 || user_usec != 0 || system_usec != 0)
  161. goto cleanup;
  162. struct cpu_hog_func_param param = {
  163. .nprocs = 1,
  164. .ts = {
  165. .tv_sec = usage_seconds,
  166. .tv_nsec = 0,
  167. },
  168. .clock_type = CPU_HOG_CLOCK_PROCESS,
  169. };
  170. if (cg_run(cpucg, hog_cpus_timed, (void *)&param))
  171. goto cleanup;
  172. usage_usec = cg_read_key_long(cpucg, "cpu.stat", "usage_usec");
  173. user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
  174. if (user_usec <= 0)
  175. goto cleanup;
  176. if (!values_close_report(usage_usec, expected_usage_usec, 1))
  177. goto cleanup;
  178. ret = KSFT_PASS;
  179. cleanup:
  180. cg_destroy(cpucg);
  181. free(cpucg);
  182. return ret;
  183. }
  184. /*
  185. * Creates a nice process that consumes CPU and checks that the elapsed
  186. * usertime in the cgroup is close to the expected time.
  187. */
  188. static int test_cpucg_nice(const char *root)
  189. {
  190. int ret = KSFT_FAIL;
  191. int status;
  192. long user_usec, nice_usec;
  193. long usage_seconds = 2;
  194. long expected_nice_usec = usage_seconds * USEC_PER_SEC;
  195. char *cpucg;
  196. pid_t pid;
  197. cpucg = cg_name(root, "cpucg_test");
  198. if (!cpucg)
  199. goto cleanup;
  200. if (cg_create(cpucg))
  201. goto cleanup;
  202. user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
  203. nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec");
  204. if (nice_usec == -1)
  205. ret = KSFT_SKIP;
  206. if (user_usec != 0 || nice_usec != 0)
  207. goto cleanup;
  208. /*
  209. * We fork here to create a new process that can be niced without
  210. * polluting the nice value of other selftests
  211. */
  212. pid = fork();
  213. if (pid < 0) {
  214. goto cleanup;
  215. } else if (pid == 0) {
  216. struct cpu_hog_func_param param = {
  217. .nprocs = 1,
  218. .ts = {
  219. .tv_sec = usage_seconds,
  220. .tv_nsec = 0,
  221. },
  222. .clock_type = CPU_HOG_CLOCK_PROCESS,
  223. };
  224. char buf[64];
  225. snprintf(buf, sizeof(buf), "%d", getpid());
  226. if (cg_write(cpucg, "cgroup.procs", buf))
  227. goto cleanup;
  228. /* Try to keep niced CPU usage as constrained to hog_cpu as possible */
  229. nice(1);
  230. hog_cpus_timed(cpucg, &param);
  231. exit(0);
  232. } else {
  233. waitpid(pid, &status, 0);
  234. if (!WIFEXITED(status))
  235. goto cleanup;
  236. user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
  237. nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec");
  238. if (!values_close_report(nice_usec, expected_nice_usec, 1))
  239. goto cleanup;
  240. ret = KSFT_PASS;
  241. }
  242. cleanup:
  243. cg_destroy(cpucg);
  244. free(cpucg);
  245. return ret;
  246. }
  247. static int
  248. run_cpucg_weight_test(
  249. const char *root,
  250. pid_t (*spawn_child)(const struct cpu_hogger *child),
  251. int (*validate)(const struct cpu_hogger *children, int num_children))
  252. {
  253. int ret = KSFT_FAIL, i;
  254. char *parent = NULL;
  255. struct cpu_hogger children[3] = {};
  256. parent = cg_name(root, "cpucg_test_0");
  257. if (!parent)
  258. goto cleanup;
  259. if (cg_create(parent))
  260. goto cleanup;
  261. if (cg_write(parent, "cgroup.subtree_control", "+cpu"))
  262. goto cleanup;
  263. for (i = 0; i < ARRAY_SIZE(children); i++) {
  264. children[i].cgroup = cg_name_indexed(parent, "cpucg_child", i);
  265. if (!children[i].cgroup)
  266. goto cleanup;
  267. if (cg_create(children[i].cgroup))
  268. goto cleanup;
  269. if (cg_write_numeric(children[i].cgroup, "cpu.weight",
  270. 50 * (i + 1)))
  271. goto cleanup;
  272. }
  273. for (i = 0; i < ARRAY_SIZE(children); i++) {
  274. pid_t pid = spawn_child(&children[i]);
  275. if (pid <= 0)
  276. goto cleanup;
  277. children[i].pid = pid;
  278. }
  279. for (i = 0; i < ARRAY_SIZE(children); i++) {
  280. int retcode;
  281. waitpid(children[i].pid, &retcode, 0);
  282. if (!WIFEXITED(retcode))
  283. goto cleanup;
  284. if (WEXITSTATUS(retcode))
  285. goto cleanup;
  286. }
  287. for (i = 0; i < ARRAY_SIZE(children); i++)
  288. children[i].usage = cg_read_key_long(children[i].cgroup,
  289. "cpu.stat", "usage_usec");
  290. if (validate(children, ARRAY_SIZE(children)))
  291. goto cleanup;
  292. ret = KSFT_PASS;
  293. cleanup:
  294. for (i = 0; i < ARRAY_SIZE(children); i++) {
  295. cg_destroy(children[i].cgroup);
  296. free(children[i].cgroup);
  297. }
  298. cg_destroy(parent);
  299. free(parent);
  300. return ret;
  301. }
  302. static pid_t weight_hog_ncpus(const struct cpu_hogger *child, int ncpus)
  303. {
  304. long usage_seconds = 10;
  305. struct cpu_hog_func_param param = {
  306. .nprocs = ncpus,
  307. .ts = {
  308. .tv_sec = usage_seconds,
  309. .tv_nsec = 0,
  310. },
  311. .clock_type = CPU_HOG_CLOCK_WALL,
  312. };
  313. return cg_run_nowait(child->cgroup, hog_cpus_timed, (void *)&param);
  314. }
  315. static pid_t weight_hog_all_cpus(const struct cpu_hogger *child)
  316. {
  317. return weight_hog_ncpus(child, get_nprocs());
  318. }
  319. static int
  320. overprovision_validate(const struct cpu_hogger *children, int num_children)
  321. {
  322. int ret = KSFT_FAIL, i;
  323. for (i = 0; i < num_children - 1; i++) {
  324. long delta;
  325. if (children[i + 1].usage <= children[i].usage)
  326. goto cleanup;
  327. delta = children[i + 1].usage - children[i].usage;
  328. if (!values_close_report(delta, children[0].usage, 35))
  329. goto cleanup;
  330. }
  331. ret = KSFT_PASS;
  332. cleanup:
  333. return ret;
  334. }
  335. /*
  336. * First, this test creates the following hierarchy:
  337. * A
  338. * A/B cpu.weight = 50
  339. * A/C cpu.weight = 100
  340. * A/D cpu.weight = 150
  341. *
  342. * A separate process is then created for each child cgroup which spawns as
  343. * many threads as there are cores, and hogs each CPU as much as possible
  344. * for some time interval.
  345. *
  346. * Once all of the children have exited, we verify that each child cgroup
  347. * was given proportional runtime as informed by their cpu.weight.
  348. */
  349. static int test_cpucg_weight_overprovisioned(const char *root)
  350. {
  351. return run_cpucg_weight_test(root, weight_hog_all_cpus,
  352. overprovision_validate);
  353. }
  354. static pid_t weight_hog_one_cpu(const struct cpu_hogger *child)
  355. {
  356. return weight_hog_ncpus(child, 1);
  357. }
  358. static int
  359. underprovision_validate(const struct cpu_hogger *children, int num_children)
  360. {
  361. int ret = KSFT_FAIL, i;
  362. for (i = 0; i < num_children - 1; i++) {
  363. if (!values_close_report(children[i + 1].usage, children[0].usage, 15))
  364. goto cleanup;
  365. }
  366. ret = KSFT_PASS;
  367. cleanup:
  368. return ret;
  369. }
  370. /*
  371. * First, this test creates the following hierarchy:
  372. * A
  373. * A/B cpu.weight = 50
  374. * A/C cpu.weight = 100
  375. * A/D cpu.weight = 150
  376. *
  377. * A separate process is then created for each child cgroup which spawns a
  378. * single thread that hogs a CPU. The testcase is only run on systems that
  379. * have at least one core per-thread in the child processes.
  380. *
  381. * Once all of the children have exited, we verify that each child cgroup
  382. * had roughly the same runtime despite having different cpu.weight.
  383. */
  384. static int test_cpucg_weight_underprovisioned(const char *root)
  385. {
  386. // Only run the test if there are enough cores to avoid overprovisioning
  387. // the system.
  388. if (get_nprocs() < 4)
  389. return KSFT_SKIP;
  390. return run_cpucg_weight_test(root, weight_hog_one_cpu,
  391. underprovision_validate);
  392. }
  393. static int
  394. run_cpucg_nested_weight_test(const char *root, bool overprovisioned)
  395. {
  396. int ret = KSFT_FAIL, i;
  397. char *parent = NULL, *child = NULL;
  398. struct cpu_hogger leaf[3] = {};
  399. long nested_leaf_usage, child_usage;
  400. int nprocs = get_nprocs();
  401. if (!overprovisioned) {
  402. if (nprocs < 4)
  403. /*
  404. * Only run the test if there are enough cores to avoid overprovisioning
  405. * the system.
  406. */
  407. return KSFT_SKIP;
  408. nprocs /= 4;
  409. }
  410. parent = cg_name(root, "cpucg_test");
  411. child = cg_name(parent, "cpucg_child");
  412. if (!parent || !child)
  413. goto cleanup;
  414. if (cg_create(parent))
  415. goto cleanup;
  416. if (cg_write(parent, "cgroup.subtree_control", "+cpu"))
  417. goto cleanup;
  418. if (cg_create(child))
  419. goto cleanup;
  420. if (cg_write(child, "cgroup.subtree_control", "+cpu"))
  421. goto cleanup;
  422. if (cg_write(child, "cpu.weight", "1000"))
  423. goto cleanup;
  424. for (i = 0; i < ARRAY_SIZE(leaf); i++) {
  425. const char *ancestor;
  426. long weight;
  427. if (i == 0) {
  428. ancestor = parent;
  429. weight = 1000;
  430. } else {
  431. ancestor = child;
  432. weight = 5000;
  433. }
  434. leaf[i].cgroup = cg_name_indexed(ancestor, "cpucg_leaf", i);
  435. if (!leaf[i].cgroup)
  436. goto cleanup;
  437. if (cg_create(leaf[i].cgroup))
  438. goto cleanup;
  439. if (cg_write_numeric(leaf[i].cgroup, "cpu.weight", weight))
  440. goto cleanup;
  441. }
  442. for (i = 0; i < ARRAY_SIZE(leaf); i++) {
  443. pid_t pid;
  444. struct cpu_hog_func_param param = {
  445. .nprocs = nprocs,
  446. .ts = {
  447. .tv_sec = 10,
  448. .tv_nsec = 0,
  449. },
  450. .clock_type = CPU_HOG_CLOCK_WALL,
  451. };
  452. pid = cg_run_nowait(leaf[i].cgroup, hog_cpus_timed,
  453. (void *)&param);
  454. if (pid <= 0)
  455. goto cleanup;
  456. leaf[i].pid = pid;
  457. }
  458. for (i = 0; i < ARRAY_SIZE(leaf); i++) {
  459. int retcode;
  460. waitpid(leaf[i].pid, &retcode, 0);
  461. if (!WIFEXITED(retcode))
  462. goto cleanup;
  463. if (WEXITSTATUS(retcode))
  464. goto cleanup;
  465. }
  466. for (i = 0; i < ARRAY_SIZE(leaf); i++) {
  467. leaf[i].usage = cg_read_key_long(leaf[i].cgroup,
  468. "cpu.stat", "usage_usec");
  469. if (leaf[i].usage <= 0)
  470. goto cleanup;
  471. }
  472. nested_leaf_usage = leaf[1].usage + leaf[2].usage;
  473. if (overprovisioned) {
  474. if (!values_close_report(leaf[0].usage, nested_leaf_usage, 15))
  475. goto cleanup;
  476. } else if (!values_close_report(leaf[0].usage * 2, nested_leaf_usage, 15))
  477. goto cleanup;
  478. child_usage = cg_read_key_long(child, "cpu.stat", "usage_usec");
  479. if (child_usage <= 0)
  480. goto cleanup;
  481. if (!values_close_report(child_usage, nested_leaf_usage, 1))
  482. goto cleanup;
  483. ret = KSFT_PASS;
  484. cleanup:
  485. for (i = 0; i < ARRAY_SIZE(leaf); i++) {
  486. cg_destroy(leaf[i].cgroup);
  487. free(leaf[i].cgroup);
  488. }
  489. cg_destroy(child);
  490. free(child);
  491. cg_destroy(parent);
  492. free(parent);
  493. return ret;
  494. }
  495. /*
  496. * First, this test creates the following hierarchy:
  497. * A
  498. * A/B cpu.weight = 1000
  499. * A/C cpu.weight = 1000
  500. * A/C/D cpu.weight = 5000
  501. * A/C/E cpu.weight = 5000
  502. *
  503. * A separate process is then created for each leaf, which spawn nproc threads
  504. * that burn a CPU for a few seconds.
  505. *
  506. * Once all of those processes have exited, we verify that each of the leaf
  507. * cgroups have roughly the same usage from cpu.stat.
  508. */
  509. static int
  510. test_cpucg_nested_weight_overprovisioned(const char *root)
  511. {
  512. return run_cpucg_nested_weight_test(root, true);
  513. }
  514. /*
  515. * First, this test creates the following hierarchy:
  516. * A
  517. * A/B cpu.weight = 1000
  518. * A/C cpu.weight = 1000
  519. * A/C/D cpu.weight = 5000
  520. * A/C/E cpu.weight = 5000
  521. *
  522. * A separate process is then created for each leaf, which nproc / 4 threads
  523. * that burns a CPU for a few seconds.
  524. *
  525. * Once all of those processes have exited, we verify that each of the leaf
  526. * cgroups have roughly the same usage from cpu.stat.
  527. */
  528. static int
  529. test_cpucg_nested_weight_underprovisioned(const char *root)
  530. {
  531. return run_cpucg_nested_weight_test(root, false);
  532. }
  533. /*
  534. * This test creates a cgroup with some maximum value within a period, and
  535. * verifies that a process in the cgroup is not overscheduled.
  536. */
  537. static int test_cpucg_max(const char *root)
  538. {
  539. int ret = KSFT_FAIL;
  540. long quota_usec = 1000;
  541. long default_period_usec = 100000; /* cpu.max's default period */
  542. long duration_seconds = 1;
  543. long duration_usec = duration_seconds * USEC_PER_SEC;
  544. long usage_usec, n_periods, remainder_usec, expected_usage_usec;
  545. char *cpucg;
  546. char quota_buf[32];
  547. snprintf(quota_buf, sizeof(quota_buf), "%ld", quota_usec);
  548. cpucg = cg_name(root, "cpucg_test");
  549. if (!cpucg)
  550. goto cleanup;
  551. if (cg_create(cpucg))
  552. goto cleanup;
  553. if (cg_write(cpucg, "cpu.max", quota_buf))
  554. goto cleanup;
  555. struct cpu_hog_func_param param = {
  556. .nprocs = 1,
  557. .ts = {
  558. .tv_sec = duration_seconds,
  559. .tv_nsec = 0,
  560. },
  561. .clock_type = CPU_HOG_CLOCK_WALL,
  562. };
  563. if (cg_run(cpucg, hog_cpus_timed, (void *)&param))
  564. goto cleanup;
  565. usage_usec = cg_read_key_long(cpucg, "cpu.stat", "usage_usec");
  566. if (usage_usec <= 0)
  567. goto cleanup;
  568. /*
  569. * The following calculation applies only since
  570. * the cpu hog is set to run as per wall-clock time
  571. */
  572. n_periods = duration_usec / default_period_usec;
  573. remainder_usec = duration_usec - n_periods * default_period_usec;
  574. expected_usage_usec
  575. = n_periods * quota_usec + MIN(remainder_usec, quota_usec);
  576. if (!values_close_report(usage_usec, expected_usage_usec, 10))
  577. goto cleanup;
  578. ret = KSFT_PASS;
  579. cleanup:
  580. cg_destroy(cpucg);
  581. free(cpucg);
  582. return ret;
  583. }
  584. /*
  585. * This test verifies that a process inside of a nested cgroup whose parent
  586. * group has a cpu.max value set, is properly throttled.
  587. */
  588. static int test_cpucg_max_nested(const char *root)
  589. {
  590. int ret = KSFT_FAIL;
  591. long quota_usec = 1000;
  592. long default_period_usec = 100000; /* cpu.max's default period */
  593. long duration_seconds = 1;
  594. long duration_usec = duration_seconds * USEC_PER_SEC;
  595. long usage_usec, n_periods, remainder_usec, expected_usage_usec;
  596. char *parent, *child;
  597. char quota_buf[32];
  598. snprintf(quota_buf, sizeof(quota_buf), "%ld", quota_usec);
  599. parent = cg_name(root, "cpucg_parent");
  600. child = cg_name(parent, "cpucg_child");
  601. if (!parent || !child)
  602. goto cleanup;
  603. if (cg_create(parent))
  604. goto cleanup;
  605. if (cg_write(parent, "cgroup.subtree_control", "+cpu"))
  606. goto cleanup;
  607. if (cg_create(child))
  608. goto cleanup;
  609. if (cg_write(parent, "cpu.max", quota_buf))
  610. goto cleanup;
  611. struct cpu_hog_func_param param = {
  612. .nprocs = 1,
  613. .ts = {
  614. .tv_sec = duration_seconds,
  615. .tv_nsec = 0,
  616. },
  617. .clock_type = CPU_HOG_CLOCK_WALL,
  618. };
  619. if (cg_run(child, hog_cpus_timed, (void *)&param))
  620. goto cleanup;
  621. usage_usec = cg_read_key_long(child, "cpu.stat", "usage_usec");
  622. if (usage_usec <= 0)
  623. goto cleanup;
  624. /*
  625. * The following calculation applies only since
  626. * the cpu hog is set to run as per wall-clock time
  627. */
  628. n_periods = duration_usec / default_period_usec;
  629. remainder_usec = duration_usec - n_periods * default_period_usec;
  630. expected_usage_usec
  631. = n_periods * quota_usec + MIN(remainder_usec, quota_usec);
  632. if (!values_close_report(usage_usec, expected_usage_usec, 10))
  633. goto cleanup;
  634. ret = KSFT_PASS;
  635. cleanup:
  636. cg_destroy(child);
  637. free(child);
  638. cg_destroy(parent);
  639. free(parent);
  640. return ret;
  641. }
  642. #define T(x) { x, #x }
  643. struct cpucg_test {
  644. int (*fn)(const char *root);
  645. const char *name;
  646. } tests[] = {
  647. T(test_cpucg_subtree_control),
  648. T(test_cpucg_stats),
  649. T(test_cpucg_nice),
  650. T(test_cpucg_weight_overprovisioned),
  651. T(test_cpucg_weight_underprovisioned),
  652. T(test_cpucg_nested_weight_overprovisioned),
  653. T(test_cpucg_nested_weight_underprovisioned),
  654. T(test_cpucg_max),
  655. T(test_cpucg_max_nested),
  656. };
  657. #undef T
  658. int main(int argc, char *argv[])
  659. {
  660. char root[PATH_MAX];
  661. int i;
  662. ksft_print_header();
  663. ksft_set_plan(ARRAY_SIZE(tests));
  664. if (cg_find_unified_root(root, sizeof(root), NULL))
  665. ksft_exit_skip("cgroup v2 isn't mounted\n");
  666. if (cg_read_strstr(root, "cgroup.subtree_control", "cpu"))
  667. if (cg_write(root, "cgroup.subtree_control", "+cpu"))
  668. ksft_exit_skip("Failed to set cpu controller\n");
  669. for (i = 0; i < ARRAY_SIZE(tests); i++) {
  670. switch (tests[i].fn(root)) {
  671. case KSFT_PASS:
  672. ksft_test_result_pass("%s\n", tests[i].name);
  673. break;
  674. case KSFT_SKIP:
  675. ksft_test_result_skip("%s\n", tests[i].name);
  676. break;
  677. default:
  678. ksft_test_result_fail("%s\n", tests[i].name);
  679. break;
  680. }
  681. }
  682. ksft_finished();
  683. }