arm related changes to util things, enabling leaky sockets and shutdown messages...
[oweals/gnunet.git] / src / util / test_os_load.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/test_os_load.c
22  * @brief testcase for util/os_load.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_configuration_lib.h"
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_disk_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_time_lib.h"
31
32 #define VERBOSE 0
33
34 static int
35 testcpu ()
36 {
37   static long k;
38   int ret;
39   struct GNUNET_TIME_Absolute start;
40   struct GNUNET_CONFIGURATION_Handle *cfg;
41
42   fprintf (stderr, "CPU load test, this may take a while.");
43   cfg = GNUNET_CONFIGURATION_create ();
44   GNUNET_assert (cfg != NULL);
45   /* need to run each phase for more than 10s since
46      statuscalls only refreshes that often... */
47   GNUNET_CONFIGURATION_set_value_number (cfg, "LOAD", "MAXCPULOAD", 100);
48   GNUNET_OS_load_cpu_get (cfg);
49   start = GNUNET_TIME_absolute_get ();
50   while ((GNUNET_TIME_absolute_get_duration (start).value < 120 * 1000) &&
51          (0 != GNUNET_OS_load_cpu_get (cfg)))
52     sleep (1);
53   start = GNUNET_TIME_absolute_get ();
54   ret = GNUNET_OS_load_cpu_get (cfg);
55   if (ret > 10)
56     {
57       fprintf (stderr,
58                "\nWARNING: base load too high (%d) to run CPU load test.\n",
59                ret);
60       GNUNET_CONFIGURATION_destroy (cfg);
61       return 0;
62     }
63   if (ret == -1)
64     {
65       fprintf (stderr, "\nWARNING: CPU load determination not supported.\n");
66       GNUNET_CONFIGURATION_destroy (cfg);
67       return 0;
68     }
69   while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
70     {
71       k++;                      /* do some processing to drive load up */
72       if (ret < GNUNET_OS_load_cpu_get (cfg))
73         break;
74     }
75   if (ret >= GNUNET_OS_load_cpu_get (cfg))
76     {
77       fprintf (stderr,
78                "\nbusy loop failed to increase CPU load: %d >= %d.",
79                ret, GNUNET_OS_load_cpu_get (cfg));
80     }
81   else
82     {
83 #if VERBOSE
84       fprintf (stderr,
85                "\nbusy loop increased CPU load: %d < %d.",
86                ret, GNUNET_OS_load_cpu_get (cfg));
87 #endif
88     }
89   fprintf (stderr, "\n");
90
91
92   GNUNET_CONFIGURATION_destroy (cfg);
93   return 0;
94 }
95
96 static int
97 testdisk ()
98 {
99   int ret;
100   struct GNUNET_DISK_FileHandle *fh;
101   char buf[65536];
102   struct GNUNET_TIME_Absolute start;
103   struct GNUNET_CONFIGURATION_Handle *cfg;
104
105   fprintf (stderr, "IO load test, this may take a while.");
106   cfg = GNUNET_CONFIGURATION_create ();
107   GNUNET_assert (cfg != NULL);
108   /* need to run each phase for more than 10s since
109      statuscalls only refreshes that often... */
110   GNUNET_CONFIGURATION_set_value_number (cfg, "LOAD", "MAXIOLOAD", 100);
111   GNUNET_OS_load_disk_get (cfg);
112   start = GNUNET_TIME_absolute_get ();
113   while ((GNUNET_TIME_absolute_get_duration (start).value < 12 * 1000) &&
114          (0 != GNUNET_OS_load_disk_get (cfg)))
115     sleep (1);
116   start = GNUNET_TIME_absolute_get ();
117   ret = GNUNET_OS_load_disk_get (cfg);
118   if (ret > 10)
119     {
120       fprintf (stderr,
121                "WARNING: base load too high (%d) to run IO load test.\n",
122                ret);
123       GNUNET_CONFIGURATION_destroy (cfg);
124       return 0;
125     }
126   if (ret == -1)
127     {
128       fprintf (stderr, "WARNING: IO load determination not supported.\n");
129       GNUNET_CONFIGURATION_destroy (cfg);
130       return 0;
131     }
132   memset (buf, 42, sizeof (buf));
133   fh = GNUNET_DISK_file_open (".loadfile", GNUNET_DISK_OPEN_WRITE
134                               | GNUNET_DISK_OPEN_CREATE,
135                               GNUNET_DISK_PERM_USER_READ |
136                               GNUNET_DISK_PERM_USER_WRITE);
137   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
138   while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
139     {
140       GNUNET_DISK_file_seek (fh,
141                              GNUNET_CRYPTO_random_u64
142                              (GNUNET_CRYPTO_QUALITY_WEAK, 1024 * 1024 * 1024),
143                              GNUNET_DISK_SEEK_SET);
144       GNUNET_assert (sizeof (buf) ==
145                      GNUNET_DISK_file_write (fh, buf, sizeof (buf)));
146       GNUNET_DISK_file_sync (fh);
147       if (ret < GNUNET_OS_load_disk_get (cfg))
148         break;
149     }
150   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
151   GNUNET_break (0 == UNLINK (".loadfile"));
152   if (ret >= GNUNET_OS_load_disk_get (cfg))
153     {
154       fprintf (stderr,
155                "\nbusy loop failed to increase IO load: %d >= %d.",
156                ret, GNUNET_OS_load_disk_get (cfg));
157       ret = 0;
158     }
159   else
160     {
161 #if VERBOSE
162       fprintf (stderr,
163                "\nbusy loop increased disk load: %d < %d.",
164                ret, GNUNET_OS_load_disk_get (cfg));
165 #endif
166       ret = 0;
167     }
168   fprintf (stderr, "\n");
169   GNUNET_CONFIGURATION_destroy (cfg);
170   return ret;
171 }
172
173 int
174 main (int argc, char *argv[])
175 {
176   int errCnt = 0;
177
178   GNUNET_log_setup ("test-os-load", "WARNING", NULL);
179   if (0 != testcpu ())
180     errCnt++;
181   if (0 != testdisk ())
182     errCnt++;
183   return errCnt;
184 }