-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathloadcheck.cc
391 lines (347 loc) · 11.9 KB
/
loadcheck.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Portions of this code are Copyright 2011 Univa Inc.
*
* Portions of this software are Copyright (c) 2023-2024 HPC-Gridware GmbH
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include "uti/ocs_topology.h"
#include "uti/sge_arch.h"
#include "uti/sge_bootstrap.h"
#include "uti/sge_language.h"
#include "uti/sge_os.h"
#include "uti/sge_rmon_macros.h"
#include "sgeobj/sge_host.h"
#include "sgeobj/sge_binding.h"
#include "basis_types.h"
#include "msg_utilbin.h"
#include <TestClass.h>
#if defined(OCS_HWLOC)
#include <sys/utsname.h>
#endif
void usage();
void print_mem_load(const char *, const char *, int, double, const char*);
void check_core_binding();
#if defined(OCS_HWLOC)
void test_hwloc();
#endif
#if defined(OCS_HWLOC)
void fill_socket_core_topology(dstring* msocket, dstring* mcore, dstring* mthread, dstring* mtopology);
#endif
void usage()
{
fprintf(stderr, "%s loadcheck [-cb] | [-int] [-loadval name]\n", MSG_UTILBIN_USAGE);
fprintf(stderr, "\t\t[-cb] \t\t\t\t Shows core binding and processor topology related information.\n");
fprintf(stderr, "\t\t[-int]\t\t\t\t Print as integer\n");
fprintf(stderr, "\t\t[-loadval]\t\t\t Select specific load value\n");
exit(1);
}
int main(int argc, char *argv[])
{
double avg[3];
int loads;
char *name;
ocs::TestClass tst("troete");
tst.method("rhabarberkuchen");
#if defined(OCS_HWLOC)
dstring msocket = DSTRING_INIT;
dstring mcore = DSTRING_INIT;
dstring mthread = DSTRING_INIT;
dstring mtopology = DSTRING_INIT;
#endif
#ifdef SGE_LOADMEM
sge_mem_info_t mem_info;
#endif
#ifdef SGE_LOADCPU
double total = 0.0;
#endif
int pos = 0, print_as_int = 0, precision, core_binding = 0;
const char *m;
DENTER_MAIN(TOP_LAYER, "loadcheck");
#ifdef __SGE_COMPILE_WITH_GETTEXT__
/* init language output for gettext() , it will use the right language */
sge_init_language_func((gettext_func_type) gettext,
(setlocale_func_type) setlocale,
(bindtextdomain_func_type) bindtextdomain,
(textdomain_func_type) textdomain);
sge_init_language(nullptr,nullptr);
#endif /* __SGE_COMPILE_WITH_GETTEXT__ */
if (argc == 2 && !strcmp(argv[1], "-cb")) {
core_binding = 1;
} else {
for (int i = 1; i < argc;) {
if (!strcmp(argv[i], "-int")) {
print_as_int = 1;
} else if (!strcmp(argv[i], "-loadval")) {
if (i + 1 < argc) {
pos = i + 1;
} else {
usage();
}
i++;
} else {
usage();
}
i++;
}
}
if (core_binding) {
check_core_binding();
DRETURN(1);
} else if (print_as_int) {
m = "";
precision = 0;
} else {
m = "M";
precision = 6;
}
if ((pos && !strcmp("arch", argv[pos])) || !pos) {
const char *arch = sge_get_arch();
printf("arch %s\n", arch);
}
if ((pos && !strcmp("num_proc", argv[pos])) || !pos) {
int nprocs = sge_nprocs();
printf("num_proc %d\n", nprocs);
}
#if defined(OCS_HWLOC)
fill_socket_core_topology(&msocket, &mcore, &mthread, &mtopology);
if ((pos && !strcmp("m_socket", argv[pos])) || !pos) {
printf("m_socket %s\n", sge_dstring_get_string(&msocket));
}
if ((pos && !strcmp("m_core", argv[pos])) || !pos) {
printf("m_core %s\n", sge_dstring_get_string(&mcore));
}
if ((pos && !strcmp("m_thread", argv[pos])) || !pos) {
printf("m_thread %s\n", sge_dstring_get_string(&mthread));
}
if ((pos && !strcmp("m_topology", argv[pos])) || !pos) {
printf("m_topology %s\n", sge_dstring_get_string(&mtopology));
}
#else
if ((pos && !strcmp("m_socket", argv[pos])) || !pos) {
printf("m_socket -\n");
}
if ((pos && !strcmp("m_core", argv[pos])) || !pos) {
printf("m_core -\n");
}
if ((pos && !strcmp("m_thread", argv[pos])) || !pos) {
printf("m_thread -\n");
}
if ((pos && !strcmp("m_topology", argv[pos])) || !pos) {
printf("m_topology -\n");
}
#endif
loads = sge_getloadavg(avg, 3);
if (loads>0 && ((pos && !strcmp("load_short", argv[pos])) || !pos)) {
printf("load_short %.2f\n", avg[0]);
}
if (loads>1 && ((pos && !strcmp("load_medium", argv[pos])) || !pos)) {
printf("load_medium %.2f\n", avg[1]);
}
if (loads>2 && ((pos && !strcmp("load_long", argv[pos])) || !pos)) {
printf("load_long %.2f\n", avg[2]);
}
if (pos) {
name = argv[pos];
} else {
name = nullptr;
}
#ifdef SGE_LOADMEM
/* memory load report */
memset(&mem_info, 0, sizeof(sge_mem_info_t));
if (sge_loadmem(&mem_info)) {
fprintf(stderr, "%s\n", MSG_SYSTEM_RETMEMORYINDICESFAILED);
#if defined(OCS_HWLOC)
sge_dstring_free(&mcore);
sge_dstring_free(&msocket);
sge_dstring_free(&mthread);
sge_dstring_free(&mtopology);
#endif
DRETURN(1);
}
print_mem_load(LOAD_ATTR_MEM_FREE, name, precision, mem_info.mem_free, m);
print_mem_load(LOAD_ATTR_SWAP_FREE, name, precision, mem_info.swap_free, m);
print_mem_load(LOAD_ATTR_VIRTUAL_FREE, name, precision, mem_info.mem_free + mem_info.swap_free, m);
print_mem_load(LOAD_ATTR_MEM_TOTAL, name, precision, mem_info.mem_total, m);
print_mem_load(LOAD_ATTR_SWAP_TOTAL, name, precision, mem_info.swap_total, m);
print_mem_load(LOAD_ATTR_VIRTUAL_TOTAL, name, precision, mem_info.mem_total + mem_info.swap_total, m);
print_mem_load(LOAD_ATTR_MEM_USED, name, precision, mem_info.mem_total - mem_info.mem_free, m);
print_mem_load(LOAD_ATTR_SWAP_USED, name, precision, mem_info.swap_total - mem_info.swap_free, m);
print_mem_load(LOAD_ATTR_VIRTUAL_USED, name, precision,(mem_info.mem_total + mem_info.swap_total) -
(mem_info.mem_free + mem_info.swap_free), m);
#endif /* SGE_LOADMEM */
#if defined(SGE_LOADCPU)
loads = sge_getcpuload(&total);
sleep(1);
loads = sge_getcpuload(&total);
if (loads != -1) {
print_mem_load("cpu", name, 1, total, "%");
}
#endif /* SGE_LOADCPU */
if ((pos && !strcmp("with_extensions", argv[pos])) || !pos) {
printf("with_extensions %d\n",
#if defined(WITH_EXTENSIONS)
1
#else
0
#endif
);
}
#if defined(OCS_HWLOC)
sge_dstring_free(&mcore);
sge_dstring_free(&msocket);
sge_dstring_free(&mthread);
sge_dstring_free(&mtopology);
#endif
return 0;
}
void print_mem_load(const char *name, const char *thisone, int precision, double value, const char *m) {
if ((thisone && !strcmp(name, thisone)) || !thisone)
printf("%-15s %.*f%s\n", name, precision, value, m);
}
/****** loadcheck/check_core_binding() *****************************************
* NAME
* check_core_binding() -- Checks core binding functionality on current host.
*
* SYNOPSIS
* void check_core_binding()
*
* FUNCTION
* Checks core binding functionality on current host.
*
* INPUTS
*
* RESULT
* void - No result
*
*******************************************************************************/
void check_core_binding()
{
/* try if it is possible to use hwloc in case of Linux */
#if defined(OCS_HWLOC)
printf("Your OGE version has built-in core binding functionality!\n");
test_hwloc();
#else
printf("Your OGE does currently not support core binding on this platform!\n");
#endif
}
#if defined(OCS_HWLOC)
void test_hwloc()
{
char* topology = nullptr;
int length = 0;
int s, c;
struct utsname name;
if (uname(&name) != -1) {
printf("Your " SFN " kernel version is: %s\n", name.sysname, name.release);
}
if (!ocs::topo_has_core_binding()) {
printf("Your " SFN " kernel seems not to offer core binding capabilities for HWLOC!\n",
name.sysname);
}
if (!ocs::topo_has_topology_information()) {
printf("No topology information could by retrieved by HWLOC!\n");
} else {
/* get amount of sockets */
printf("Amount of sockets:\t\t%d\n", ocs::topo_get_total_amount_of_sockets());
/* get amount of cores */
printf("Amount of cores:\t\t%d\n", ocs::topo_get_total_amount_of_cores());
/* the amount of threads must be shown as well */
printf("Amount of threads:\t\t%d\n", ocs::topo_get_total_amount_of_threads());
/* get topology */
ocs::topo_get_topology(&topology, &length);
printf("Topology:\t\t\t%s\n", topology);
sge_free(&topology);
printf("Mapping of logical socket and core numbers to internal\n");
/* for each socket,core pair get the internal processor number */
/* try multi-mapping */
for (s = 0; s < ocs::topo_get_total_amount_of_sockets(); s++) {
for (c = 0; c < ocs::topo_get_amount_of_cores_for_socket(s); c++) {
int* proc_ids = nullptr;
int amount = 0;
if (ocs::topo_get_processor_ids(s, c, &proc_ids, &amount)) {
printf("Internal processor ids for socket %5d core %5d: ", s , c);
for (int i = 0; i < amount; i++) {
printf(" %5d", proc_ids[i]);
}
printf("\n");
delete[] proc_ids;
} else {
printf("Couldn't get processor ids for socket %5d core %5d\n", s, c);
}
}
}
}
}
#endif
#if defined(OCS_HWLOC)
/****** loadcheck/fill_socket_core_topology() **********************************
* NAME
* fill_socket_core_topology() -- Get load values regarding processor topology.
*
* SYNOPSIS
* void fill_socket_core_topology(dstring* msocket, dstring* mcore, dstring*
* mtopology)
*
* FUNCTION
* Gets the values regarding processor topology.
*
* OUTPUTS
* dstring* msocket - The amount of sockets the host have.
* dstring* mcore - The amount of cores the host have.
* dstring* mtopology - The topology the host have.
*
* RESULT
* void - nothing
*
*******************************************************************************/
void fill_socket_core_topology(dstring* msocket, dstring* mcore, dstring* mthread, dstring* mtopology)
{
int ms, mc, mt;
char* topo = nullptr;
int length = 0;
ms = get_execd_amount_of_sockets();
mc = get_execd_amount_of_cores();
mt = get_execd_amount_of_threads();
if (!get_execd_topology(&topo, &length) || topo == nullptr) {
topo = sge_strdup(nullptr, "-");
}
sge_dstring_sprintf(msocket, "%d", ms);
sge_dstring_sprintf(mcore, "%d", mc);
sge_dstring_sprintf(mthread, "%d", mt);
sge_dstring_append(mtopology, topo);
sge_free(&topo);
}
#endif