stats.c (3212B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: stats.c 8 * Purpose: cpu occupation stats. 9 */ 10 11 12 #include <sys/types.h> 13 #include <sal/core/stats.h> 14 #include <sal/core/libc.h> 15 16 #if defined(linux) 17 #include <stdio.h> 18 #endif 19 20 21 #if defined(linux) 22 STATIC uint64 sal_atouint64(CONST char *str){ 23 uint64 val = COMPILER_64_INIT(0,0); 24 25 while(*str >= '0' && *str <= '9') { 26 COMPILER_64_UMUL_32(val, 10); 27 COMPILER_64_ADD_32(val, *str - '0'); 28 ++str; 29 } 30 return val; 31 } 32 33 STATIC CONST char *advance_to_next_number(CONST char *str) { 34 while(*str && (*str < '0' || *str > '9')) { 35 ++str; 36 } 37 return (*str) ? str : NULL; 38 } 39 40 #define ADVANCE_TO_NEXT_NUMBER_RETURN_FALSE_ON_ERROR(_cur_location, _fp)\ 41 _cur_location = advance_to_next_number(_cur_location); \ 42 if(!_cur_location) { \ 43 fclose(_fp); \ 44 return FALSE; \ 45 } 46 47 #define TAKE_NUMBER_AND_ADVANCE(_str, _number) \ 48 _number = sal_atouint64(_str); \ 49 while(*_str && (*_str >= '0' || *_str <= '9')) { \ 50 ++_str; \ 51 } 52 53 54 int 55 sal_cpu_stats_get(sal_cpu_stats_t* cpu_stats) 56 { 57 FILE* f; 58 uint64 nice = COMPILER_64_INIT(0,0); 59 uint64 iowait = COMPILER_64_INIT(0,0); 60 char line[200]; 61 CONST char *seperator; 62 63 COMPILER_64_ZERO(cpu_stats->user); 64 COMPILER_64_ZERO(cpu_stats->kernel); 65 COMPILER_64_ZERO(cpu_stats->idle); 66 COMPILER_64_ZERO(cpu_stats->total); 67 68 if ((f=fopen("/proc/stat", "r" )) == NULL) { 69 return FALSE; 70 } 71 72 fgets(line, sizeof(line), f); 73 seperator = sal_strstr(line, "cpu"); 74 if(!seperator) { 75 fclose(f); 76 return FALSE; 77 } 78 79 ADVANCE_TO_NEXT_NUMBER_RETURN_FALSE_ON_ERROR(seperator, f); 80 TAKE_NUMBER_AND_ADVANCE(seperator, cpu_stats->user); 81 ADVANCE_TO_NEXT_NUMBER_RETURN_FALSE_ON_ERROR(seperator, f); 82 TAKE_NUMBER_AND_ADVANCE(seperator, nice); 83 ADVANCE_TO_NEXT_NUMBER_RETURN_FALSE_ON_ERROR(seperator, f); 84 TAKE_NUMBER_AND_ADVANCE(seperator, cpu_stats->kernel); 85 ADVANCE_TO_NEXT_NUMBER_RETURN_FALSE_ON_ERROR(seperator, f); 86 TAKE_NUMBER_AND_ADVANCE(seperator, cpu_stats->idle); 87 ADVANCE_TO_NEXT_NUMBER_RETURN_FALSE_ON_ERROR(seperator, f); 88 TAKE_NUMBER_AND_ADVANCE(seperator, iowait); 89 90 fclose(f); 91 92 COMPILER_64_ADD_64(cpu_stats->kernel, nice); 93 COMPILER_64_ADD_64(cpu_stats->kernel, iowait); 94 95 COMPILER_64_ADD_64(cpu_stats->total, cpu_stats->user); 96 COMPILER_64_ADD_64(cpu_stats->total, cpu_stats->idle); 97 COMPILER_64_ADD_64(cpu_stats->total, cpu_stats->kernel); 98 99 return TRUE; 100 } /* sal_cpu_stats_get */ 101 102 #else 103 104 int 105 sal_cpu_stats_get(sal_cpu_stats_t* cpu_stats) 106 { 107 COMPILER_64_ZERO(cpu_stats->user); 108 COMPILER_64_ZERO(cpu_stats->kernel); 109 COMPILER_64_ZERO(cpu_stats->idle); 110 COMPILER_64_ZERO(cpu_stats->total); 111 112 return FALSE; 113 } 114 #endif 115