xref: /netbsd-src/external/mit/libuv/dist/test/test-platform-output.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 
27 TEST_IMPL(platform_output) {
28 /* TODO(gengjiawen): Fix test on QEMU. */
29 #if defined(__QEMU__)
30   RETURN_SKIP("Test does not currently work in QEMU");
31 #endif
32 
33   char buffer[512];
34   size_t rss;
35   size_t size;
36   double uptime;
37   uv_pid_t pid;
38   uv_pid_t ppid;
39   uv_rusage_t rusage;
40   uv_cpu_info_t* cpus;
41   uv_interface_address_t* interfaces;
42   uv_passwd_t pwd;
43   uv_utsname_t uname;
44   int count;
45   int i;
46   int err;
47 
48   err = uv_get_process_title(buffer, sizeof(buffer));
49   ASSERT(err == 0);
50   printf("uv_get_process_title: %s\n", buffer);
51 
52   size = sizeof(buffer);
53   err = uv_cwd(buffer, &size);
54   ASSERT(err == 0);
55   printf("uv_cwd: %s\n", buffer);
56 
57   err = uv_resident_set_memory(&rss);
58 #if defined(__MSYS__)
59   ASSERT(err == UV_ENOSYS);
60 #else
61   ASSERT(err == 0);
62   printf("uv_resident_set_memory: %llu\n", (unsigned long long) rss);
63 #endif
64 
65   err = uv_uptime(&uptime);
66 #if defined(__PASE__)
67   ASSERT(err == UV_ENOSYS);
68 #else
69   ASSERT(err == 0);
70   ASSERT(uptime > 0);
71   printf("uv_uptime: %f\n", uptime);
72 #endif
73 
74   err = uv_getrusage(&rusage);
75   ASSERT(err == 0);
76   ASSERT(rusage.ru_utime.tv_sec >= 0);
77   ASSERT(rusage.ru_utime.tv_usec >= 0);
78   ASSERT(rusage.ru_stime.tv_sec >= 0);
79   ASSERT(rusage.ru_stime.tv_usec >= 0);
80   printf("uv_getrusage:\n");
81   printf("  user: %llu sec %llu microsec\n",
82          (unsigned long long) rusage.ru_utime.tv_sec,
83          (unsigned long long) rusage.ru_utime.tv_usec);
84   printf("  system: %llu sec %llu microsec\n",
85          (unsigned long long) rusage.ru_stime.tv_sec,
86          (unsigned long long) rusage.ru_stime.tv_usec);
87   printf("  page faults: %llu\n", (unsigned long long) rusage.ru_majflt);
88   printf("  maximum resident set size: %llu\n",
89          (unsigned long long) rusage.ru_maxrss);
90 
91   err = uv_cpu_info(&cpus, &count);
92 #if defined(__CYGWIN__) || defined(__MSYS__)
93   ASSERT(err == UV_ENOSYS);
94 #else
95   ASSERT(err == 0);
96 
97   printf("uv_cpu_info:\n");
98   for (i = 0; i < count; i++) {
99     printf("  model: %s\n", cpus[i].model);
100     printf("  speed: %d\n", cpus[i].speed);
101     printf("  times.sys: %llu\n", (unsigned long long) cpus[i].cpu_times.sys);
102     printf("  times.user: %llu\n",
103            (unsigned long long) cpus[i].cpu_times.user);
104     printf("  times.idle: %llu\n",
105            (unsigned long long) cpus[i].cpu_times.idle);
106     printf("  times.irq: %llu\n",  (unsigned long long) cpus[i].cpu_times.irq);
107     printf("  times.nice: %llu\n",
108            (unsigned long long) cpus[i].cpu_times.nice);
109   }
110 #endif
111   uv_free_cpu_info(cpus, count);
112 
113   err = uv_interface_addresses(&interfaces, &count);
114   ASSERT(err == 0);
115 
116   printf("uv_interface_addresses:\n");
117   for (i = 0; i < count; i++) {
118     printf("  name: %s\n", interfaces[i].name);
119     printf("  internal: %d\n", interfaces[i].is_internal);
120     printf("  physical address: ");
121     printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
122            (unsigned char)interfaces[i].phys_addr[0],
123            (unsigned char)interfaces[i].phys_addr[1],
124            (unsigned char)interfaces[i].phys_addr[2],
125            (unsigned char)interfaces[i].phys_addr[3],
126            (unsigned char)interfaces[i].phys_addr[4],
127            (unsigned char)interfaces[i].phys_addr[5]);
128 
129     if (interfaces[i].address.address4.sin_family == AF_INET) {
130       uv_ip4_name(&interfaces[i].address.address4, buffer, sizeof(buffer));
131     } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
132       uv_ip6_name(&interfaces[i].address.address6, buffer, sizeof(buffer));
133     }
134 
135     printf("  address: %s\n", buffer);
136 
137     if (interfaces[i].netmask.netmask4.sin_family == AF_INET) {
138       uv_ip4_name(&interfaces[i].netmask.netmask4, buffer, sizeof(buffer));
139       printf("  netmask: %s\n", buffer);
140     } else if (interfaces[i].netmask.netmask4.sin_family == AF_INET6) {
141       uv_ip6_name(&interfaces[i].netmask.netmask6, buffer, sizeof(buffer));
142       printf("  netmask: %s\n", buffer);
143     } else {
144       printf("  netmask: none\n");
145     }
146   }
147   uv_free_interface_addresses(interfaces, count);
148 
149   err = uv_os_get_passwd(&pwd);
150   ASSERT(err == 0);
151 
152   printf("uv_os_get_passwd:\n");
153   printf("  euid: %ld\n", pwd.uid);
154   printf("  gid: %ld\n", pwd.gid);
155   printf("  username: %s\n", pwd.username);
156   printf("  shell: %s\n", pwd.shell);
157   printf("  home directory: %s\n", pwd.homedir);
158 
159   pid = uv_os_getpid();
160   ASSERT(pid > 0);
161   printf("uv_os_getpid: %d\n", (int) pid);
162   ppid = uv_os_getppid();
163   ASSERT(ppid > 0);
164   printf("uv_os_getppid: %d\n", (int) ppid);
165 
166   err = uv_os_uname(&uname);
167   ASSERT(err == 0);
168   printf("uv_os_uname:\n");
169   printf("  sysname: %s\n", uname.sysname);
170   printf("  release: %s\n", uname.release);
171   printf("  version: %s\n", uname.version);
172   printf("  machine: %s\n", uname.machine);
173 
174   return 0;
175 }
176