crabs_tuxcpu-1.1.1 ©2004-2011 - Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com)
1 /* 2 **============================================================================= 3 ** crabs_tuxcpu-1.1.1 : Pingouin animé (occupation CPU) 4 ** Copyright (C) 2004-2011 : Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com) 5 ** 6 ** Ce source fait partie d'un projet logiciel libre. Vous pouvez le distribuer 7 ** et/ou le modifier en respectant les termes de la GNU General Public License 8 ** version 2 ou (suite a votre propre choix) une version ulterieure. 9 ** 10 ** Ce programme est distribue dans l'espoir qu'il puisse etre utile, mais 11 ** sans aucune garantie, meme si il est associe a un produit qui vous en 12 ** propose une. Conformez-vous a la GNU General Public License pour avoir 13 ** plus de precisions. 14 ** 15 ** L'auteur ne peut etre tenu responsable de l'utilisation faite des 16 ** composantes associees a ce projet (en partie ou dans leur totalite). 17 ** 18 ** Une copie du fichier de la GNU GPL est fournie dans le repertoire DOC 19 ** de ce projet sous le nom gnu_gpl.txt 20 ** 21 **============================================================================= 22 */ 23 #include "anim.qh" 24 25 // return a - b : can work with integer overflow 26 static double UINTdiff( unsigned int a, unsigned int b ) 27 { 28 unsigned int max, res ; 29 if ( a >= b ) return (double)( a - b ) ; 30 max = 0 ; max-- ; 31 res = max - b ; 32 res = res + a + 1 ; 33 return (double)res ; 34 } 35 int PerCentAnim::cpu_usage( bool init ) 36 { 37 #ifdef LINUX 38 unsigned int ab, ac, ad, ae, t, u ; 39 FILE* f_stat = fopen("/proc/stat", "r"); 40 fscanf(f_stat, "%*s %u %u %u %u", &ab, &ac, &ad, &ae); 41 fclose(f_stat); 42 u = ab+ad+ac ; 43 t = u+ae ; 44 if ( init ) 45 { 46 total_use = t ; 47 user_use = u ; 48 for(int i=0; i<5; i++ ) tuse[i] = 0 ; pos=0 ; 49 return 0 ; 50 } 51 double tt, tu, use ; 52 tt = UINTdiff( t, total_use ) ; 53 tu = UINTdiff( u, user_use ) ; 54 if ( tt == 0.0 ) 55 { int prec = (pos+4)%5 ; use = tuse[prec] ; } 56 else 57 use = tu / tt * 100 ; 58 total_use = t ; user_use = u ; 59 tuse[pos] = (int)use ; pos = (pos+1)%5 ; 60 int sum=0;for(int i=0;i<5;i++) sum+=tuse[i] ; 61 #ifdef BIG_DEBUG 62 char b[1024] ; 63 sprintf( b, "%u+%u+%u=%u +%u=%u %f/%f=%f%% [%d]=%d ret=%d (%d)\n", 64 ab, ad, ac, u, ae, t, tu, tt, use, pos, (int)use, sum/5, 65 (sum/5)*(nb-1) / 100 ) ; 66 write( 1, b, strlen(b) ) ; 67 #endif 68 return sum/5 ; 69 #else 70 static bool increment = true ; 71 static int value = 0 ; 72 if ( increment ) 73 { 74 value ++ ; 75 if ( value >= nb ) 76 { 77 value = nb-1 ; 78 increment = false ; 79 } 80 } 81 else 82 { 83 value -- ; 84 if ( value < 0 ) 85 { 86 value = 1 ; 87 increment = true ; 88 } 89 } 90 return (value*100)/(nb-1) ; 91 #endif 92 }
Date de génération : 22/09/2011 21:48