@@ -311,67 +311,94 @@ int get_present_core_list(int **present_cores, int *num_present_cores, int threa
311
311
}
312
312
313
313
static void print_cpu_list (const cpu_set_t * cpuset , int cpuset_size ,
314
- int cpus_in_system )
314
+ int threads_per_cpu )
315
315
{
316
- int core ;
316
+ int * present_cores = NULL ;
317
+ int num_present_cores ;
318
+ int start , end , i = 0 ;
317
319
const char * comma = "" ;
318
320
319
- for (core = 0 ; core < cpus_in_system ; core ++ ) {
320
- int begin = core ;
321
- if (CPU_ISSET_S (core , cpuset_size , cpuset )) {
322
- while (CPU_ISSET_S (core + 1 , cpuset_size , cpuset ))
323
- core ++ ;
321
+ if (get_present_core_list (& present_cores , & num_present_cores , threads_per_cpu ) != 0 ) {
322
+ fprintf (stderr , "Failed to get present_cores list\n" );
323
+ return ;
324
+ }
324
325
325
- if (core > begin )
326
- printf ("%s%d-%d" , comma , begin , core );
327
- else
328
- printf ("%s%d" , comma , core );
326
+ while (i < num_present_cores ) {
327
+ start = present_cores [i ];
328
+ if (CPU_ISSET_S (start , cpuset_size , cpuset )) {
329
+ end = start ;
330
+ while (i + 1 < num_present_cores &&
331
+ CPU_ISSET_S (present_cores [i + 1 ], cpuset_size , cpuset ) &&
332
+ present_cores [i + 1 ] == end + 1 ) {
333
+ end = present_cores [++ i ];
334
+ }
335
+ if (start == end ) {
336
+ printf ("%s%d" , comma , start );
337
+ } else {
338
+ printf ("%s%d-%d" , comma , start , end );
339
+ }
329
340
comma = "," ;
330
341
}
342
+ i ++ ;
331
343
}
344
+ free (present_cores );
332
345
}
333
346
334
- int __do_smt (bool numeric , int cpus_in_system , int threads_per_cpu ,
335
- bool print_smt_state )
347
+ int __do_smt (bool numeric , int cpus_in_system , int threads_per_cpu , bool print_smt_state )
336
348
{
337
- int thread , c , smt_state = 0 ;
338
349
cpu_set_t * * cpu_states = NULL ;
339
- int cpu_state_size = CPU_ALLOC_SIZE ( cpus_in_system ) ;
340
- int start_cpu = 0 , stop_cpu = cpus_in_system ;
350
+ int thread , smt_state = -1 ;
351
+ int cpu_state_size ;
341
352
int rc = 0 ;
353
+ int i , core_id , threads_online ;
354
+ int * present_cores = NULL ;
355
+ int num_present_cores ;
342
356
343
- cpu_states = ( cpu_set_t * * ) calloc ( threads_per_cpu , sizeof ( cpu_set_t ));
344
- if (! cpu_states )
357
+ if ( get_present_core_list ( & present_cores , & num_present_cores , threads_per_cpu ) != 0 ) {
358
+ fprintf ( stderr , "Failed to get present core list\n" );
345
359
return - ENOMEM ;
360
+ }
361
+ cpu_state_size = CPU_ALLOC_SIZE (num_present_cores );
362
+ cpu_states = (cpu_set_t * * )calloc (threads_per_cpu , sizeof (cpu_set_t * ));
363
+ if (!cpu_states ) {
364
+ rc = - ENOMEM ;
365
+ goto cleanup_present_cores ;
366
+ }
346
367
347
368
for (thread = 0 ; thread < threads_per_cpu ; thread ++ ) {
348
- cpu_states [thread ] = CPU_ALLOC (cpus_in_system );
369
+ cpu_states [thread ] = CPU_ALLOC (num_present_cores );
370
+ if (!cpu_states [thread ]) {
371
+ rc = - ENOMEM ;
372
+ goto cleanup_cpu_states ;
373
+ }
349
374
CPU_ZERO_S (cpu_state_size , cpu_states [thread ]);
350
375
}
351
376
352
- for (c = start_cpu ; c < stop_cpu ; c ++ ) {
353
- int threads_online = __get_one_smt_state ( c , threads_per_cpu ) ;
354
-
377
+ for (i = 0 ; i < num_present_cores ; i ++ ) {
378
+ core_id = present_cores [ i ] ;
379
+ threads_online = __get_one_smt_state ( core_id , threads_per_cpu );
355
380
if (threads_online < 0 ) {
356
381
rc = threads_online ;
357
- goto cleanup_get_smt ;
382
+ goto cleanup_cpu_states ;
383
+ }
384
+ if (threads_online ) {
385
+ CPU_SET_S (core_id , cpu_state_size , cpu_states [threads_online - 1 ]);
358
386
}
359
- if (threads_online )
360
- CPU_SET_S (c , cpu_state_size ,
361
- cpu_states [threads_online - 1 ]);
362
387
}
363
388
364
389
for (thread = 0 ; thread < threads_per_cpu ; thread ++ ) {
365
390
if (CPU_COUNT_S (cpu_state_size , cpu_states [thread ])) {
366
- if (smt_state == 0 )
391
+ if (smt_state == -1 )
367
392
smt_state = thread + 1 ;
368
393
else if (smt_state > 0 )
369
394
smt_state = 0 ; /* mix of SMT modes */
370
395
}
371
396
}
372
397
373
- if (!print_smt_state )
374
- return smt_state ;
398
+ if (!print_smt_state ) {
399
+ rc = smt_state ;
400
+ goto cleanup_cpu_states ;
401
+ }
375
402
376
403
if (smt_state == 1 ) {
377
404
if (numeric )
@@ -380,21 +407,22 @@ int __do_smt(bool numeric, int cpus_in_system, int threads_per_cpu,
380
407
printf ("SMT is off\n" );
381
408
} else if (smt_state == 0 ) {
382
409
for (thread = 0 ; thread < threads_per_cpu ; thread ++ ) {
383
- if (CPU_COUNT_S (cpu_state_size ,
384
- cpu_states [thread ])) {
410
+ if (CPU_COUNT_S (cpu_state_size , cpu_states [thread ])) {
385
411
printf ("SMT=%d: " , thread + 1 );
386
- print_cpu_list (cpu_states [thread ],
387
- cpu_state_size , cpus_in_system );
412
+ print_cpu_list (cpu_states [thread ], cpu_state_size , threads_per_cpu );
388
413
printf ("\n" );
389
414
}
390
415
}
391
416
} else {
392
417
printf ("SMT=%d\n" , smt_state );
393
418
}
394
419
395
- cleanup_get_smt :
420
+ cleanup_cpu_states :
396
421
for (thread = 0 ; thread < threads_per_cpu ; thread ++ )
397
422
CPU_FREE (cpu_states [thread ]);
423
+ free (cpu_states );
424
+ cleanup_present_cores :
425
+ free (present_cores );
398
426
399
427
return rc ;
400
428
}
0 commit comments