Debian Codegraph @

inst/testme/test-cgroups.R

raw
1 library(parallelly)
2
3 message("*** Internal functions ...")
4
5 ## Reset memoization
6 parallelly:::maxCores(NA_integer_)
7 parallelly:::procPath(NA_character_)
8 getUID <- parallelly:::getUID
9 environment(getUID)$.uid <- NULL
10
11 message("getUID(): ", getUID())
12 message("getUID(): ", getUID())
13
14 message("getCGroupsVersion(): ", parallelly:::getCGroupsVersion())
15 message("getCGroupsVersion(): ", parallelly:::getCGroupsVersion())
16
17 message("getCGroupsRoot(): ", parallelly:::getCGroupsRoot())
18 message("getCGroupsRoot(): ", parallelly:::getCGroupsRoot())
19
20 options(parallelly.cgroups.cpuset = 0:3)
21 message("getCGroups1CpuSet(): ", paste(parallelly:::getCGroups1CpuSet(), collapse = ", "))
22 options(parallelly.cgroups.cpuset = NULL)
23
24 options(parallelly.cgroups.cpuquota = 2.5)
25 message("getCGroups1CpuQuota(): ", parallelly:::getCGroups1CpuQuota())
26 options(parallelly.cgroups.cpuquota = NULL)
27
28 options(parallelly.cgroups2.cpuset.cpus = 0:3)
29 message("getCGroups2CpuSet(): ", paste(parallelly:::getCGroups2CpuSet(), collapse = ", "))
30 message("getCGroups2CpuSet('cpuset.cpus'): ", paste(parallelly:::getCGroups2CpuSet("cpuset.cpus"), collapse = ", "))
31 options(parallelly.cgroups2.cpuset.cpus = NULL)
32
33 options(parallelly.cgroups2.cpuset.cpus.effective = 0:3)
34 message("getCGroups2CpuSet('cpuset.cpus.effective'): ", paste(parallelly:::getCGroups2CpuSet("cpuset.cpus.effective"), collapse = ", "))
35 options(parallelly.cgroups2.cpuset.cpus.effective = NULL)
36
37 options(parallelly.cgroups2.cpu.max = 100000L)
38 message("getCGroups2CpuMax(): ", parallelly:::getCGroups2CpuMax())
39 options(parallelly.cgroups2.cpu.max = NULL)
40
41 local({
42 opwd <- setwd(tempdir())
43 on.exit(setwd(opwd))
44 tarfile <- parallelly:::cloneCGroups()
45 print(file.info(tarfile))
46 stopifnot(utils::file_test("-f", tarfile))
47 file.remove(tarfile)
48 })
49
50 message("*** Internal functions ... DONE")
51
52
53
54 message("*** cgroups ...")
55
56 message("- getCGroups()")
57 cgroups <- parallelly:::getCGroups()
58 print(cgroups)
59 stopifnot(
60 is.data.frame(cgroups),
61 identical(colnames(cgroups), c("hierarchy_id", "controller", "path")),
62 nrow(cgroups) == 0L || !is.null(cgroups$controller)
63 )
64
65 message("- getCGroupsRoot()")
66 root <- parallelly:::getCGroupsRoot()
67 cat(sprintf("cgroups root path: %s\n", sQuote(root)))
68 stopifnot(length(root) == 1L, is.character(root))
69
70 message("- getCGroups()")
71 cgroups <- parallelly:::getCGroups()
72 print(cgroups)
73 stopifnot(is.data.frame(cgroups))
74
75 message("- getCGroupsPath()")
76 path <- parallelly:::getCGroupsPath("cpu")
77 cat(sprintf("cgroups 'cpu' path: %s\n", sQuote(path)))
78 stopifnot(length(path) == 1L, is.character(path))
79
80 path <- parallelly:::getCGroupsPath("cpuset")
81 cat(sprintf("cgroups 'cpuset' path: %s\n", sQuote(path)))
82 stopifnot(length(path) == 1L, is.character(path))
83
84
85 message("- getCGroups1Value()")
86 value <- parallelly:::getCGroups1Value("cpu", "cpu.cfs_quota_us")
87 cat(sprintf("cgroups v1 'cpu.cfs_quota_us' value: %s\n", sQuote(value)))
88 stopifnot(length(value) == 1L, is.character(value))
89
90 value <- parallelly:::getCGroups1Value("cpu", "cpu.cfs_total_us")
91 cat(sprintf("cgroups v1 'cpu.cfs_total_us' value: %s\n", sQuote(value)))
92 stopifnot(length(value) == 1L, is.character(value))
93
94 value <- parallelly:::getCGroups1Value("cpuset", "cpuset.cpus")
95 cat(sprintf("cgroups v1 'cpuset.cpus' value: %s\n", sQuote(value)))
96 stopifnot(length(value) == 1L, is.character(value))
97
98
99 message("- getCGroups1CpuSet()")
100 value <- parallelly:::getCGroups1CpuSet()
101 cat(sprintf("CPU set: [n=%d] %s\n", length(value), paste(sQuote(value), collapse = ", ")))
102 stopifnot(length(value) >= 0L, is.integer(value), !any(is.na(value)))
103
104
105 message("- getCGroups1CpuQuotaMicroseconds()")
106 value <- parallelly:::getCGroups1CpuQuotaMicroseconds()
107 cat(sprintf("CPU quota (ms): %d\n", value))
108 stopifnot(
109 length(value) == 1L,
110 is.integer(value),
111 is.na(value) || value == -1 || value > 0
112 )
113
114 message("- getCGroups1CpuPeriodMicroseconds()")
115 value <- parallelly:::getCGroups1CpuPeriodMicroseconds()
116 cat(sprintf("CPU total (ms): %d\n", value))
117 stopifnot(
118 length(value) == 1L,
119 is.integer(value),
120 is.na(value) || value > 0
121 )
122
123 message("- getCGroups1CpuQuota()")
124 value <- parallelly:::getCGroups1CpuQuota()
125 cat(sprintf("CPU quota (ratio): %g\n", value))
126 stopifnot(
127 length(value) == 1L,
128 is.numeric(value),
129 !is.infinite(value),
130 is.na(value) || value > 0
131 )
132
133 message("- getCGroups2CpuSet()")
134 value <- parallelly:::getCGroups2CpuSet()
135 cat(sprintf("CPU set: [n=%d] %s\n", length(value), paste(sQuote(value), collapse = ", ")))
136 stopifnot(length(value) >= 0L, is.integer(value), !any(is.na(value)))
137
138 message("- getCGroups2CpuSet('cpuset.cpus.effective')")
139 value <- parallelly:::getCGroups2CpuSet("cpuset.cpus.effective")
140 cat(sprintf("CPU set: [n=%d] %s\n", length(value), paste(sQuote(value), collapse = ", ")))
141 stopifnot(length(value) >= 0L, is.integer(value), !any(is.na(value)))
142
143 message("- getCGroups2CpuMax()")
144 value <- parallelly:::getCGroups2CpuMax()
145 cat(sprintf("CPU quota (ratio): %g\n", value))
146 stopifnot(
147 length(value) == 1L,
148 is.numeric(value),
149 !is.infinite(value),
150 is.na(value) || value > 0
151 )
152
153 message("- availableCores(methods = 'cgroups2.cpuset.cpus')")
154 n <- availableCores(methods = "cgroups2.cpuset.cpus", na.rm = FALSE)
155 cat(sprintf("Number of cores: %s\n", n))
156 stopifnot(length(n) == 1L, is.integer(n), is.na(n) || n >= 1L)
157
158 message("- availableCores(methods = 'cgroups2.cpuset.cpus.effective')")
159 n <- availableCores(methods = "cgroups2.cpuset.cpus.effective", na.rm = FALSE)
160 cat(sprintf("Number of cores: %s\n", n))
161 stopifnot(length(n) == 1L, is.integer(n), is.na(n) || n >= 1L)
162
163 message("*** cgroups ... DONE")
164
165 root <- system.file(package = "parallelly", "test-data", mustWork = TRUE)
166 for (dir in c("no-cgroups", "mixed-cgroups", "cgroups1", "cgroups2")) {
167 message(sprintf("%s - real-world ...", dir))
168 path <- file.path(root, dir)
169 stopifnot(file_test("-d", path))
170 tarballs <- dir(path = path, pattern = ".*[.]tar[.]gz", full.names = TRUE)
171 names(tarballs) <- sub("[.]tar[.]gz$", "", basename(tarballs))
172
173 for (name in names(tarballs)) {
174 parallelly:::withCGroups(tarballs[name], {
175 file <- file.path(path, sprintf("%s.R", name))
176 if (file_test("-f", file)) {
177 message("Running custom test script: ", sQuote(file))
178 source(file, local = FALSE)
179 }
180 })
181 }
182 message(sprintf("%s - real-world ... done", dir))
183 }
184
185 message("cgroups - real-world ... DONE")