Orbiter 2022
Combinatorial Objects
os_interface.cpp
Go to the documentation of this file.
1/*
2 * os_interface.cpp
3 *
4 * Created on: Sep 28, 2019
5 * Author: betten
6 */
7
8
9
10
11
12#include "foundations.h"
13
14using namespace std;
15
16#include <cstdio>
17#include <sys/types.h>
18#ifdef SYSTEMUNIX
19#include <unistd.h>
20#endif
21#include <fcntl.h>
22
23#ifdef SYSTEMUNIX
24#ifndef SYSTEMWINDOWS
25#include <sys/times.h>
26 /* for times() */
27#endif
28#endif
29#include <time.h>
30 /* for time() */
31#ifdef SYSTEMWINDOWS
32#include <io.h>
33#include <process.h>
34#endif
35#ifdef SYSTEM_IS_MACINTOSH
36//#include <console.h>
37#include <time.h> // for clock()
38//#include <unix.h>
39#endif
40#ifdef MSDOS
41#include <time.h> // for clock()
42#endif
43
44
45namespace orbiter {
46namespace layer1_foundations {
47namespace orbiter_kernel_system {
48
49
50
51
53{
54 *l = 0;
55#ifdef SYSTEMUNIX
56#ifndef SYSTEMWINDOWS
57 struct tms *buffer = (struct tms *) malloc(sizeof(struct tms));
58 times(buffer);
59 *l = (long) buffer->tms_utime;
60 free(buffer);
61#endif
62#endif
63#ifdef SYSTEM_IS_MACINTOSH
64 *l = 0;
65#endif
66#ifdef MSDOS
67 *l = (long) clock();
68#endif /* MSDOS */
69}
70
71
73{
74#ifdef SYSTEM_IS_MACINTOSH
75#if 0
76 struct task_basic_info t_info;
77 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
78
79 if (KERN_SUCCESS != task_info(mach_task_self(),
80 TASK_BASIC_INFO, (task_info_t)&t_info,
81 &t_info_count))
82 {
83 cout << "os_interface::os_memory_usage error in task_info" << endl;
84 exit(1);
85 }
86 // resident size is in t_info.resident_size;
87 // virtual size is in t_info.virtual_size;
88
89
90 //cout << "resident_size=" << t_info.resident_size << endl;
91 //cout << "virtual_size=" << t_info.virtual_size << endl;
92 return t_info.resident_size;
93#endif
94#endif
95#ifdef SYSTEM_LINUX
96 int chars = 128;
97 // number of characters to read from the
98 // /proc/self/status file in a given line
99 FILE* file = fopen("/proc/self/status", "r");
100 char line[chars];
101 while (fgets(line, chars, file) != NULL) {
102 // read one line at a time
103 if (strncmp(line, "VmPeak:", 7) == 0) {
104 // compare the first 7 characters of every line
105 char* p = line + 7;
106 // start reading from the 7th index of the line
107 p[strlen(p)-3] = '\0';
108 // set the null terminator at the beginning of size units
109 fclose(file);
110 // close the file stream
111 return atoi(p);
112 // return the size in KiB
113 }
114 }
115#endif
116 return 0;
117}
118
120{
121#ifdef SYSTEMUNIX
122#ifndef SYSTEMWINDOWS
123 struct tms tms_buffer;
124 int t;
125
126 if (-1 == (int) times(&tms_buffer))
127 return(-1);
128 t = tms_buffer.tms_utime;
129 //cout << "os_ticks " << t << endl;
130 return t;
131#endif
132#endif
133#ifdef SYSTEM_IS_MACINTOSH
134#if 0
135 clock_t t;
136
137 t = clock();
138 return((int)t);
139#endif
140#endif
141#ifdef SYSTEMWINDOWS
142 return 0;
143#endif
144}
145
146static int f_system_time_set = FALSE;
147static int system_time0 = 0;
148
150{
151 int t;
152
153 t = time(NULL);
154 if (!f_system_time_set) {
155 f_system_time_set = TRUE;
156 system_time0 = t;
157 }
158 //t -= system_time0;
159 //t *= os_ticks_per_second();
160 return t;
161}
162
164{
165 static int f_tps_computed = FALSE;
166 static int tps = 0;
167#ifdef SYSTEMUNIX
168#ifndef SYSTEMWINDOWS
169 int clk_tck = 1;
170
171 if (f_tps_computed)
172 return tps;
173 else {
174 clk_tck = sysconf(_SC_CLK_TCK);
175 tps = clk_tck;
176 f_tps_computed = TRUE;
177 //cout << endl << "clock ticks per second = " << tps << endl;
178 return(clk_tck);
179 }
180#endif
181#endif
182#ifdef SYSTEMWINDOWS
183 return 1;
184#endif
185}
186
188 int tps, int &d, int &h, int &m, int &s)
189{
190 int l1;
191 int f_v = FALSE;
192
193 if (f_v) {
194 cout << "os_ticks_to_dhms ticks = " << ticks << endl;
195 }
196 l1 = ticks / tps;
197 if (f_v) {
198 cout << "os_ticks_to_dhms l1 = " << l1 << endl;
199 }
200 s = l1 % 60;
201 if (f_v) {
202 cout << "os_ticks_to_dhms s = " << s << endl;
203 }
204 l1 /= 60;
205 m = l1 % 60;
206 if (f_v) {
207 cout << "os_ticks_to_dhms m = " << m << endl;
208 }
209 l1 /= 60;
210 h = l1;
211 if (f_v) {
212 cout << "os_ticks_to_dhms h = " << h << endl;
213 }
214 if (h >= 24) {
215 d = h / 24;
216 h = h % 24;
217 }
218 else
219 d = 0;
220 if (f_v) {
221 cout << "os_ticks_to_dhms d = " << d << endl;
222 }
223}
224
225void os_interface::time_check_delta(ostream &ost, int dt)
226{
227 int tps, d, h, min, s;
228
229 tps = os_ticks_per_second();
230 //cout << "time_check_delta tps=" << tps << endl;
231 os_ticks_to_dhms(dt, tps, d, h, min, s);
232 if (d == 0 && h == 0 && min == 0 && s == 0) {
233 ost << (double) dt / (double) tps << " of a second, dt=" << dt << " tps = " << tps << endl;
234 }
235 else {
236 if ((dt / tps) >= 1) {
237 print_elapsed_time(ost, d, h, min, s);
238 }
239 else {
240 ost << "0:00";
241 }
242 }
243}
244
245void os_interface::print_elapsed_time(ostream &ost, int d, int h, int m, int s)
246{
247 if (d > 0) {
248 ost << d << "-" << h << ":" << m << ":" << s;
249 }
250 else if (h > 0) {
251 ost << h << ":" << m << ":" << s;
252 }
253 else {
254 ost << m << ":" << s;
255 }
256
257}
258
259void os_interface::time_check(ostream &ost, int t0)
260{
261 int t1, dt;
262
263 t1 = os_ticks();
264 dt = t1 - t0;
265 //cout << "time_check t0=" << t0 << endl;
266 //cout << "time_check t1=" << t1 << endl;
267 //cout << "time_check dt=" << dt << endl;
268 time_check_delta(ost, dt);
269}
270
272{
273 int t1, dt;
274
275 t1 = os_ticks();
276 dt = t1 - t0;
277 return dt;
278}
279
280
282{
283 srand((unsigned int) time(0));
284}
285
287{
288 srand((unsigned int) seed);
289}
290
292// computes a random integer r with $0 \le r < p.$
293{
294 int n;
295
296 if (p == 0) {
297 cout << "random_integer p = 0" << endl;
298 exit(1);
299 }
300 n = (int)(((double)rand() * (double)p / RAND_MAX)) % p;
301 return n;
302}
303
305{
306 int a;
307
308 {
309 ofstream fp("b");
310 fp << "#!/bin/bash" << endl;
311 fp << "echo $(date +%s)" << endl;
312 }
313 system("chmod ugo+x b");
314 system("./b >a");
315 {
316 char str[1000];
317
318 ifstream f1("a");
319 f1.getline(str, sizeof(str));
320 sscanf(str, "%d", &a);
321 }
322 return a;
323}
324
325void os_interface::get_string_from_command_line(std::string &p, int argc, std::string *argv,
326 int &i, int verbose_level)
327{
329
330 if (ST.stringcmp(argv[i], "-long_string") == 0) {
331 i++;
332 p.assign("");
333 while (TRUE) {
334 if (ST.stringcmp(argv[i], "-end_string") == 0) {
335 i++;
336 break;
337 }
338 p.append(argv[i]);
339 i++;
340 }
341 }
342 else {
343 p.assign(argv[i]);
344 i++;
345 }
346}
347
348static const char *ascii_code = "abcdefghijklmnop";
349
350static int f_has_swap_initialized = FALSE;
351static int f_has_swap = 0;
352 // indicates if char swap is present
353 // i.e., little endian / big endian
354
356{
357 //unsigned long test_long = 0x11223344L;
358 int_4 test = 0x11223344L;
359 char *ptr;
360
361 ptr = (char *) &test;
362 if (ptr[0] == 0x44) {
363 f_has_swap = TRUE;
364 cout << "we have a swap" << endl;
365 }
366 else if (ptr[0] == 0x11) {
367 f_has_swap = FALSE;
368 cout << "we don't have a swap" << endl;
369 }
370 else {
371 cout << "The test_swap test is inconclusive" << endl;
372 exit(1);
373 }
374 f_has_swap_initialized = TRUE;
375}
376
377// block_swap_chars:
378// switches the chars in the buffer pointed to by "ptr".
379// There are "no" intervals of size "size".
380// This routine is due to Roland Grund
381
382void os_interface::block_swap_chars(char *ptr, int size, int no)
383{
384 char *ptr_end, *ptr_start;
385 char chr;
386 int i;
387
388 if (!f_has_swap_initialized) {
389 test_swap();
390 }
391 if ((f_has_swap) && (size > 1)) {
392
393 for (; no--; ) {
394
395 ptr_start = ptr;
396 ptr_end = ptr_start + (size - 1);
397 for (i = size / 2; i--; ) {
398 chr = *ptr_start;
399 *ptr_start++ = *ptr_end;
400 *ptr_end-- = chr;
401 }
402 ptr += size;
403 }
404 }
405}
406
408{
409 int_4 ii = i;
410
411 //cout << "code_int4 " << i << endl;
412 uchar *q = (uchar *) &ii;
413 //block_swap_chars((SCHAR *)&ii, 4, 1);
414 code_uchar(p, q[0]);
415 code_uchar(p, q[1]);
416 code_uchar(p, q[2]);
417 code_uchar(p, q[3]);
418}
419
421{
422 int_4 ii;
423 uchar *q = (uchar *) &ii;
424 decode_uchar(p, q[0]);
425 decode_uchar(p, q[1]);
426 decode_uchar(p, q[2]);
427 decode_uchar(p, q[3]);
428 //block_swap_chars((SCHAR *)&ii, 4, 1);
429 //cout << "decode_int4 " << ii << endl;
430 return ii;
431}
432
434{
435 //cout << "code_uchar " << (int) a << endl;
436 int a_high = a >> 4;
437 int a_low = a & 15;
438 *p++ = ascii_code[a_high];
439 *p++ = ascii_code[a_low];
440}
441
443{
444 int a_high = (int)(*p++ - 'a');
445 int a_low = (int)(*p++ - 'a');
446 int i;
447 //cout << "decode_uchar a_high = " << a_high << endl;
448 //cout << "decode_uchar a_low = " << a_low << endl;
449 i = (a_high << 4) | a_low;
450 //cout << "decode_uchar i = " << i << endl;
451 //cout << "decode_uchar " << (int) i << endl;
452 a = (uchar)i;
453}
454
455
456void os_interface::get_date(std::string &str)
457{
458#ifndef SYSTEMWINDOWS
459 char s[1024];
460
461 system("date >a");
462 {
463 ifstream f1("a");
464 f1.getline(s, sizeof(s));
465 }
466 str.assign(s);
467#else
468 str.assign("unknown");
469#endif
470}
471
473{
474 cout << "test_typedefs()" << endl;
475 cout << "sizeof(int)=" << sizeof(int) << endl;
476 cout << "sizeof(long int)=" << sizeof(long int) << endl;
477 if (sizeof(int_2) != 2) {
478 cout << "warning: sizeof(int_2)=" << sizeof(int_2) << endl;
479 }
480 if (sizeof(int_4) != 4) {
481 cout << "warning: sizeof(int4)=" << sizeof(int_4) << endl;
482 }
483 if (sizeof(int_8) != 8) {
484 cout << "warning: sizeof(int8)=" << sizeof(int_8) << endl;
485 }
486 if (sizeof(uint_2) != 2) {
487 cout << "warning: sizeof(uint_2)=" << sizeof(uint_2) << endl;
488 }
489 if (sizeof(uint_4) != 4) {
490 cout << "warning: sizeof(uint_2)=" << sizeof(uint_4) << endl;
491 }
492 if (sizeof(uint_8) != 8) {
493 cout << "warning: sizeof(uint_2)=" << sizeof(uint_8) << endl;
494 }
495 cout << "test_typedefs done" << endl;
496}
497
498
499}}}
500
functions related to strings and character arrays
void get_string_from_command_line(std::string &p, int argc, std::string *argv, int &i, int verbose_level)
void print_elapsed_time(std::ostream &ost, int d, int h, int m, int s)
void os_ticks_to_dhms(int ticks, int tps, int &d, int &h, int &m, int &s)
unsigned long uint_8
Definition: foundations.h:185
unsigned char uchar
Definition: foundations.h:204
unsigned short uint_2
Definition: foundations.h:183
int int_4
Definition: foundations.h:181
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
unsigned int uint_4
Definition: foundations.h:184
short int_2
Definition: foundations.h:180
long int_8
Definition: foundations.h:182
the orbiter library for the classification of combinatorial objects