xref: /libuv/test/test-platform-output.c (revision 91ba1305)
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 #include <string.h>
25 
26 
TEST_IMPL(platform_output)27 TEST_IMPL(platform_output) {
28   char buffer[512];
29   size_t rss;
30   size_t size;
31   double uptime;
32   uv_pid_t pid;
33   uv_pid_t ppid;
34   uv_rusage_t rusage;
35   uv_cpu_info_t* cpus;
36   uv_interface_address_t* interfaces;
37   uv_passwd_t pwd;
38   uv_group_t grp;
39   uv_utsname_t uname;
40   unsigned par;
41   char* const* member;
42   int count;
43   int i;
44   int err;
45 
46   err = uv_get_process_title(buffer, sizeof(buffer));
47   ASSERT_OK(err);
48   printf("uv_get_process_title: %s\n", buffer);
49 
50   size = sizeof(buffer);
51   err = uv_cwd(buffer, &size);
52   ASSERT_OK(err);
53   printf("uv_cwd: %s\n", buffer);
54 
55   err = uv_resident_set_memory(&rss);
56 #if defined(__MSYS__)
57   ASSERT_EQ(err, UV_ENOSYS);
58 #else
59   ASSERT_OK(err);
60   printf("uv_resident_set_memory: %llu\n", (unsigned long long) rss);
61 #endif
62 
63   err = uv_uptime(&uptime);
64 #if defined(__PASE__)
65   ASSERT_EQ(err, UV_ENOSYS);
66 #else
67   ASSERT_OK(err);
68   ASSERT_GT(uptime, 0);
69   printf("uv_uptime: %f\n", uptime);
70 #endif
71 
72   err = uv_getrusage(&rusage);
73   ASSERT_OK(err);
74   ASSERT_GE(rusage.ru_utime.tv_sec, 0);
75   ASSERT_GE(rusage.ru_utime.tv_usec, 0);
76   ASSERT_GE(rusage.ru_stime.tv_sec, 0);
77   ASSERT_GE(rusage.ru_stime.tv_usec, 0);
78   printf("uv_getrusage:\n");
79   printf("  user: %llu sec %llu microsec\n",
80          (unsigned long long) rusage.ru_utime.tv_sec,
81          (unsigned long long) rusage.ru_utime.tv_usec);
82   printf("  system: %llu sec %llu microsec\n",
83          (unsigned long long) rusage.ru_stime.tv_sec,
84          (unsigned long long) rusage.ru_stime.tv_usec);
85   printf("  page faults: %llu\n", (unsigned long long) rusage.ru_majflt);
86   printf("  maximum resident set size: %llu\n",
87          (unsigned long long) rusage.ru_maxrss);
88 
89   par = uv_available_parallelism();
90   ASSERT_GE(par, 1);
91   printf("uv_available_parallelism: %u\n", par);
92 
93 #ifdef __linux__
94   FILE* file;
95   int cgroup_version = 0;
96   unsigned int cgroup_par = 0;
97   uint64_t quota, period;
98 
99   // Attempt to parse cgroup v2 to deduce parallelism constraints
100   file = fopen("/sys/fs/cgroup/cpu.max", "r");
101   if (file) {
102     if (fscanf(file, "%lu %lu", &quota, &period) == 2 && quota > 0) {
103       cgroup_version = 2;
104       cgroup_par = (unsigned int)(quota / period);
105     }
106     fclose(file);
107   }
108 
109   // If cgroup v2 wasn't present, try parsing cgroup v1
110   if (cgroup_version == 0) {
111     file = fopen("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us", "r");
112     if (file) {
113       if (fscanf(file, "%lu", &quota) == 1 && quota > 0 && quota < ~0ULL) {
114         fclose(file);
115         file = fopen("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us", "r");
116         if (file && fscanf(file, "%lu", &period) == 1) {
117           cgroup_version = 1;
118           cgroup_par = (unsigned int)(quota / period);
119         }
120       }
121       if (file) fclose(file);
122     }
123   }
124 
125   // If we found cgroup parallelism constraints, assert and print them
126   if (cgroup_par > 0) {
127     ASSERT_GE(par, cgroup_par);
128     printf("cgroup v%d available parallelism: %u\n", cgroup_version, cgroup_par);
129   }
130 #endif
131 
132   err = uv_cpu_info(&cpus, &count);
133 #if defined(__CYGWIN__) || defined(__MSYS__)
134   ASSERT_EQ(err, UV_ENOSYS);
135 #else
136   ASSERT_OK(err);
137 
138   printf("uv_cpu_info:\n");
139   for (i = 0; i < count; i++) {
140     printf("  model: %s\n", cpus[i].model);
141     printf("  speed: %d\n", cpus[i].speed);
142     printf("  times.sys: %llu\n", (unsigned long long) cpus[i].cpu_times.sys);
143     printf("  times.user: %llu\n",
144            (unsigned long long) cpus[i].cpu_times.user);
145     printf("  times.idle: %llu\n",
146            (unsigned long long) cpus[i].cpu_times.idle);
147     printf("  times.irq: %llu\n",  (unsigned long long) cpus[i].cpu_times.irq);
148     printf("  times.nice: %llu\n",
149            (unsigned long long) cpus[i].cpu_times.nice);
150   }
151 #endif
152   uv_free_cpu_info(cpus, count);
153 
154   err = uv_interface_addresses(&interfaces, &count);
155   ASSERT_OK(err);
156 
157   printf("uv_interface_addresses:\n");
158   for (i = 0; i < count; i++) {
159     printf("  name: %s\n", interfaces[i].name);
160     printf("  internal: %d\n", interfaces[i].is_internal);
161     printf("  physical address: ");
162     printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
163            (unsigned char)interfaces[i].phys_addr[0],
164            (unsigned char)interfaces[i].phys_addr[1],
165            (unsigned char)interfaces[i].phys_addr[2],
166            (unsigned char)interfaces[i].phys_addr[3],
167            (unsigned char)interfaces[i].phys_addr[4],
168            (unsigned char)interfaces[i].phys_addr[5]);
169 
170     if (interfaces[i].address.address4.sin_family == AF_INET) {
171       uv_ip4_name(&interfaces[i].address.address4, buffer, sizeof(buffer));
172     } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
173       uv_ip6_name(&interfaces[i].address.address6, buffer, sizeof(buffer));
174     }
175 
176     printf("  address: %s\n", buffer);
177 
178     if (interfaces[i].netmask.netmask4.sin_family == AF_INET) {
179       uv_ip4_name(&interfaces[i].netmask.netmask4, buffer, sizeof(buffer));
180       printf("  netmask: %s\n", buffer);
181     } else if (interfaces[i].netmask.netmask4.sin_family == AF_INET6) {
182       uv_ip6_name(&interfaces[i].netmask.netmask6, buffer, sizeof(buffer));
183       printf("  netmask: %s\n", buffer);
184     } else {
185       printf("  netmask: none\n");
186     }
187   }
188   uv_free_interface_addresses(interfaces, count);
189 
190   err = uv_os_get_passwd(&pwd);
191   ASSERT_OK(err);
192 
193   err = uv_os_get_group(&grp, pwd.gid);
194 #if defined(_WIN32)
195   ASSERT_EQ(err, UV_ENOTSUP);
196   ASSERT_EQ(pwd.uid, (unsigned long) -1);
197   ASSERT_EQ(pwd.gid, (unsigned long) -1);
198   (void) member;
199   grp.groupname = "ENOTSUP";
200 #else
201   ASSERT_OK(err);
202   ASSERT_EQ(pwd.gid, grp.gid);
203 #endif
204 
205   printf("uv_os_get_passwd:\n");
206   printf("  euid: %ld\n", pwd.uid);
207   printf("  gid: %ld (%s)\n", pwd.gid, grp.groupname);
208 #if !defined(_WIN32)
209   printf("    members: [");
210   for (member = grp.members; *member != NULL; member++) {
211     printf(" %s", *member);
212   }
213   printf(" ]\n");
214 #endif
215   printf("  username: %s\n", pwd.username);
216   if (pwd.shell != NULL) /* Not set on Windows */
217     printf("  shell: %s\n", pwd.shell);
218   printf("  home directory: %s\n", pwd.homedir);
219   uv_os_free_passwd(&pwd);
220 #if !defined(_WIN32)
221   uv_os_free_group(&grp);
222 #endif
223 
224   pid = uv_os_getpid();
225   ASSERT_GT(pid, 0);
226   printf("uv_os_getpid: %d\n", (int) pid);
227   ppid = uv_os_getppid();
228   ASSERT_GT(ppid, 0);
229   printf("uv_os_getppid: %d\n", (int) ppid);
230 
231   err = uv_os_uname(&uname);
232   ASSERT_OK(err);
233   printf("uv_os_uname:\n");
234   printf("  sysname: %s\n", uname.sysname);
235   printf("  release: %s\n", uname.release);
236   printf("  version: %s\n", uname.version);
237   printf("  machine: %s\n", uname.machine);
238 
239   return 0;
240 }
241