1 /**
2  * Copyright (c) 2009, Sun Microsystems Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * - Neither the name of Sun Microsystems Inc. nor the names of its contributors
14  *   may be used to endorse or promote products derived from this software
15  *   without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_TIME_H
31 #include <sys/time.h>
32 #endif
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
35 #endif
36 
37 #include <assert.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42 #if defined(_WIN32)
43 #include <WinError.h>
44 #endif
45 
46 #include "sigar.h"
47 #include "sigar_private.h"
48 #include "sigar_format.h"
49 #include "sigar_tests.h"
50 
51 static char *given_path;
52 
TESTnull53 TEST(test_sigar_disk_stats) {
54     sigar_file_system_list_t fslist;
55     sigar_disk_usage_t dsu;
56     size_t i;
57     int best_match = 0;
58 
59     if (!given_path) {
60         given_path = "";
61     }
62 
63     memset(&dsu, 0, sizeof(sigar_disk_usage_t));
64     assert(SIGAR_OK == sigar_file_system_list_get(t, &fslist));
65     assert(fslist.number > 0);
66 
67     for (i = 0; i < fslist.number; i++) {
68         sigar_file_system_t fs = fslist.data[i];
69         sigar_file_system_usage_t fsusage;
70         sigar_disk_usage_t diskusage;
71         int ret;
72 
73         assert(fs.dir_name);
74         assert(fs.dev_name);
75         assert(fs.type_name);
76         assert(fs.sys_type_name);
77         assert(fs.type);
78 
79         if (SIGAR_OK != (ret = sigar_file_system_ping(t, &fs))) {
80             continue;
81         }
82 
83         if (SIGAR_OK == (ret = sigar_file_system_usage_get(t, fs.dir_name, &fsusage))) {
84             if (!given_path[0]) {
85                 fprintf(stdout, "DirName=%s, ", fs.dir_name);
86             }
87             assert(IS_IMPL_U64(fsusage.total));
88             assert(IS_IMPL_U64(fsusage.free));
89             assert(IS_IMPL_U64(fsusage.used));
90             assert(IS_IMPL_U64(fsusage.avail));
91 #if !(defined(SIGAR_TEST_OS_SOLARIS) || defined(_WIN32))
92             /* solaris 8 */
93             assert(IS_IMPL_U64(fsusage.files));
94 #endif
95             assert(fsusage.use_percent >= 0);
96         } else {
97             switch (ret) {
98                 /* track the expected error code */
99 #if defined(_WIN32)
100             case ERROR_NOT_READY:
101                 break;
102 #endif
103             default:
104                 fprintf(stderr, "sigar_file_system_usage_get(%s) ret = %d (%s)\n",
105                         fs.dir_name,
106                         ret, sigar_strerror(t, ret));
107                 assert(ret == SIGAR_OK);
108                 break;
109             }
110         }
111 
112         if (SIGAR_OK == (ret = sigar_disk_usage_get(t, fs.dev_name, &diskusage))) {
113             assert(IS_IMPL_U64(diskusage.reads));
114             assert(IS_IMPL_U64(diskusage.writes));
115 #if !defined(SIGAR_TEST_OS_DARWIN)
116             /* freebsd */
117             assert(IS_IMPL_U64(diskusage.read_bytes));
118             assert(IS_IMPL_U64(diskusage.write_bytes));
119             assert(IS_IMPL_U64(diskusage.rtime));
120             assert(IS_IMPL_U64(diskusage.wtime));
121 #endif
122 #if !(defined(SIGAR_TEST_OS_LINUX) || defined(SIGAR_TEST_OS_DARWIN) || defined(_WIN32))
123             /* depending on the Linux version they might not be set */
124             assert(IS_IMPL_U64(diskusage.qtime));
125 #endif
126 #if !(defined(SIGAR_TEST_OS_LINUX) || defined(SIGAR_TEST_OS_DARWIN))
127             assert(IS_IMPL_U64(diskusage.time));
128 #endif
129 #if !defined(SIGAR_TEST_OS_DARWIN)
130             assert(IS_IMPL_U64(diskusage.snaptime));
131 #endif
132 #if 0
133             /* is -1 if undefined */
134             assert(diskusage.service_time >= 0);
135             assert(diskusage.queue >= 0);
136 #endif
137             if (given_path[0]) {
138                 if (strstr(given_path, fs.dir_name)) {
139                     size_t len = strlen(fs.dir_name);
140                     if (len > best_match) {
141                         best_match = len;
142                         dsu = diskusage;
143                     }
144                 }
145             } else {
146                 fprintf(stdout, "Read=" SIGAR_F_U64 ", Write=" SIGAR_F_U64
147                         ", Queue=" SIGAR_F_U64 ", Time=" SIGAR_F_U64
148                         ", SnapTime=" SIGAR_F_U64
149                         ", Service Time=%g, QueueDepth=%g\n",
150                         diskusage.rtime, diskusage.wtime, diskusage.qtime,
151                         diskusage.time,
152                         diskusage.snaptime, diskusage.service_time,
153                         diskusage.queue);
154             }
155         } else {
156             switch (ret) {
157             case ESRCH: /* macosx */
158             case ENXIO: /* solaris */
159             case ENOENT: /* aix */
160             case SIGAR_ENOTIMPL: /* hpux */
161                 /* track the expected error code */
162                 fprintf(stderr, "sigar_disk_usage_get(%s) ret = %d (%s)\n",
163                         fs.dev_name,
164                         ret, sigar_strerror(t, ret));
165                 break;
166             default:
167                 fprintf(stderr, "sigar_disk_usage_get(%s) ret = %d (%s)\n",
168                         fs.dev_name,
169                         ret, sigar_strerror(t, ret));
170                 assert(ret == SIGAR_OK);
171                 break;
172             }
173         }
174     }
175 
176     if (best_match) {
177         fprintf(stdout, "Read=" SIGAR_F_U64 ", Write=" SIGAR_F_U64
178                 ", Queue=" SIGAR_F_U64 ", Time=" SIGAR_F_U64
179                 ", SnapTime=" SIGAR_F_U64
180                 ", Service Time=%g, QueueDepth=%g\n",
181                 dsu.rtime, dsu.wtime, dsu.qtime,
182                 dsu.time, dsu.snaptime, dsu.service_time,
183                 dsu.queue);
184     }
185 
186     sigar_file_system_list_destroy(t, &fslist);
187 
188     return 0;
189 }
190 
main(int argc, char **argv)191 int main(int argc, char **argv) {
192     sigar_t *t;
193     int err = 0;
194 
195     assert(SIGAR_OK == sigar_open(&t));
196 
197     if (argc > 1) {
198         given_path = argv[1];
199     }
200 
201     test_sigar_disk_stats(t);
202 
203     sigar_close(t);
204 
205     return err ? -1 : 0;
206 }
207