Ignore:
Timestamp:
Dec 25, 2004, 6:43:07 PM (19 years ago)
Author:
altba\rryu
Message:

パフォーマンスカウンター系のクラスの実装を追加した。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MacFaceLibrary/CPUUsageCounter.cs

    r24 r25  
    33 */
    44using System;
     5using System.Diagnostics;
    56
    67namespace MacFace
     
    910        /// HostStatistics ‚ÌŠT—v‚Ìà–¾‚Å‚·B
    1011        /// </summary>
    11         public class HostStatistics
     12        public class CPUUsageCounter
    1213        {
    13                 public HostStatistics()
     14                private PerformanceCounter userCounter;
     15                private PerformanceCounter systemCounter;
     16                private PerformanceCounter idleCounter;
     17
     18                public CPUUsageCounter()
    1419                {
    15                         //
    16                         // TODO: ƒRƒ“ƒXƒgƒ‰ƒNƒ^ ƒƒWƒbƒN‚ð‚±‚±‚ɒljÁ‚µ‚Ä‚­‚¾‚³‚¢B
    17                         //
     20                        userCounter = new PerformanceCounter();
     21                        userCounter.CategoryName = "Processor";
     22                        userCounter.CounterName = "% User Time";
     23                        userCounter.InstanceName = "_Total";
     24
     25                        systemCounter = new PerformanceCounter();
     26                        systemCounter.CategoryName = "Processor";
     27                        systemCounter.CounterName = "% Privileged Time";
     28                        systemCounter.InstanceName = "_Total";
     29
     30                        idleCounter = new PerformanceCounter();
     31                        idleCounter.CategoryName = "Processor";
     32                        idleCounter.CounterName = "% Idle Time";
     33                        idleCounter.InstanceName = "_Total";
    1834                }
     35
     36                public CPUUsage CurrentUsage()
     37                {
     38                        int user   = (int)userCounter.NextValue();
     39                        int system = (int)systemCounter.NextValue();
     40                        int idle   = (int)idleCounter.NextValue();
     41
     42                        return new CPUUsage(user, system, idle);
     43                }
     44
    1945        }
    2046}
    21 
    22 /*
    23 
    24 typedef struct {
    25     int freePages;
    26     int activePages;
    27     int inactivePages;
    28     int wirePages;
    29     int faults;
    30     int pageins;
    31     int pageouts;
    32     unsigned long userTicks;
    33     unsigned long systemTicks;
    34     unsigned long idleTicks;
    35     unsigned long niceTicks;
    36 } HostStatData;
    37 
    38 typedef struct {
    39     int incount;
    40     int outcount;
    41 } Pageio;
    42 
    43 typedef struct {
    44     float user;
    45     float system;
    46     float idle;
    47     float nice;
    48 } CPUUsage;
    49 
    50 typedef struct {
    51     HostStatData stat;
    52     Pageio pageio;
    53     CPUUsage usage;
    54 } StatisticsRecord;
    55 
    56 @interface HostStatistics : NSObject
    57 {
    58     StatisticsRecord *ringBuffer;
    59     int bufMaxLen;
    60     int bufHead;
    61     int bufTail;
    62     int bufLen;
    63     int totalPages;
    64     int minUsedPages;
    65     int maxUsedPages;
    66 }
    67 
    68 + (unsigned int)vmPageSize;
    69 + (NSString*)kernelVersion;
    70 + (void)getStatistics:(HostStatData*)data;
    71 
    72 - (id)initWithCapacity:(unsigned)capacity;
    73 - (void)update;
    74 - (int)totalPages;
    75 - (int)minUsedPages;
    76 - (int)maxUsedPages;
    77 - (int)length;
    78 - (const StatisticsRecord*)indexAt:(unsigned)index;
    79 - (const StatisticsRecord*)head;
    80 - (const StatisticsRecord*)tail;
    81 @end
    82 
    83 #import <mach/mach.h>
    84 #import <mach/mach_types.h>
    85 #import "HostStatistics.h"
    86 
    87 //
    88 //
    89 static void hostStatistics(HostStatData *data)
    90 {
    91     vm_statistics_data_t vm_stat;
    92     mach_msg_type_number_t vm_count = HOST_VM_INFO_COUNT;
    93     host_cpu_load_info_data_t load_info;
    94     mach_msg_type_number_t load_info_count = HOST_CPU_LOAD_INFO_COUNT;
    95     host_t host = mach_host_self();
    96 
    97     host_statistics(host,HOST_VM_INFO,(host_info_t)&vm_stat, &vm_count);
    98     host_statistics(host,HOST_CPU_LOAD_INFO,(host_info_t)&load_info, &load_info_count);
    99 
    100     data->freePages = vm_stat.free_count;
    101     data->activePages = vm_stat.active_count;
    102     data->inactivePages = vm_stat.inactive_count;
    103     data->wirePages = vm_stat.wire_count;
    104     data->faults = vm_stat.faults;
    105     data->pageins = vm_stat.pageins;
    106     data->pageouts = vm_stat.pageouts;
    107 
    108     data->userTicks = load_info.cpu_ticks[CPU_STATE_USER];
    109     data->systemTicks = load_info.cpu_ticks[CPU_STATE_SYSTEM];
    110     data->idleTicks = load_info.cpu_ticks[CPU_STATE_IDLE];
    111     data->niceTicks = load_info.cpu_ticks[CPU_STATE_NICE];
    112 }
    113 
    114 @implementation HostStatistics
    115 
    116 //
    117 // ‰¼‘z‹L‰¯‚̃y[ƒWƒTƒCƒY‚ð•Ô‚·
    118 //
    119 + (unsigned int)vmPageSize
    120 {
    121     vm_size_t page_size;
    122     host_page_size(mach_host_self(),&page_size);
    123     return page_size;
    124 }
    125 
    126 //
    127 // ƒJ[ƒlƒ‹‚̃o[ƒWƒ‡ƒ“•¶Žš—ñ‚ð•Ô‚·
    128 //
    129 + (NSString*)kernelVersion
    130 {
    131     kernel_version_t kver;
    132     host_kernel_version(mach_host_self(),kver);
    133     return [NSString stringWithUTF8String:kver];
    134 }
    135 
    136 //
    137 // Œ»Ý‚Ì“Œvî•ñ‚ðÝ’è‚·‚é
    138 //
    139 + (void)getStatistics:(HostStatData*)data
    140 {
    141     hostStatistics(data);
    142 }
    143 
    144 //
    145 // ‰Šú‰»
    146 //   capacity: ƒŠƒ“ƒOƒoƒbƒtƒ@‚Ì—e—Ê
    147 //
    148 - (id)initWithCapacity:(unsigned)capacity
    149 {
    150     StatisticsRecord *rec;
    151 
    152     ringBuffer = calloc(sizeof(StatisticsRecord),capacity);
    153     rec = &ringBuffer[0];
    154     hostStatistics(&rec->stat);
    155     rec->usage.user = 0;
    156     rec->usage.system = 0;
    157     rec->usage.idle = 100.0;
    158     rec->usage.nice = 0;
    159 
    160     bufMaxLen = capacity;
    161     bufHead = 0;
    162     bufTail = 0;
    163     bufLen = 1;
    164 
    165     totalPages = rec->stat.wirePages + rec->stat.activePages + rec->stat.inactivePages + rec->stat.freePages;
    166     minUsedPages = rec->stat.wirePages + rec->stat.activePages;
    167     maxUsedPages = minUsedPages;
    168 
    169     return self;
    170 }
    171 
    172 //
    173 // ŒãŽn––
    174 //
    175 - (void)dealloc
    176 {
    177     free(ringBuffer);
    178     [super dealloc];
    179 }
    180 
    181 //
    182 // —š—ð‚̍XV
    183 //
    184 - (void)update
    185 {
    186     StatisticsRecord *rec, *lastRec;
    187     int user,sys,idle,nice,total;
    188     int usedPages;
    189 
    190     lastRec = &ringBuffer[bufHead];
    191     if (bufLen < bufMaxLen) bufLen++;
    192     if (++bufHead >= bufLen) bufHead = 0;
    193     if (bufHead == bufTail)
    194         if (++bufTail >= bufLen) bufTail = 0;
    195     rec = &ringBuffer[bufHead];
    196     hostStatistics(&rec->stat);
    197 
    198     rec->pageio.incount = rec->stat.pageins - lastRec->stat.pageins;
    199     rec->pageio.outcount = rec->stat.pageouts - lastRec->stat.pageouts;
    200 
    201     user = rec->stat.userTicks - lastRec->stat.userTicks;
    202     sys = rec->stat.systemTicks - lastRec->stat.systemTicks;
    203     idle = rec->stat.idleTicks - lastRec->stat.idleTicks;
    204     nice = rec->stat.niceTicks - lastRec->stat.niceTicks;
    205     total = user + sys + idle + nice;
    206 
    207     if (total > 0) {
    208         rec->usage.user = user * 100.0 / total;
    209         rec->usage.system = sys * 100.0 / total;
    210         rec->usage.idle = idle * 100.0 / total;
    211         rec->usage.nice = nice * 100.0 / total;
    212     } else {
    213         rec->usage = lastRec->usage;
    214     }
    215 
    216     usedPages = rec->stat.wirePages + rec->stat.activePages;
    217     if( minUsedPages < usedPages) minUsedPages = usedPages;
    218     if( maxUsedPages > usedPages) maxUsedPages = usedPages;
    219 }
    220 
    221 - (int)totalPages { return totalPages; }
    222 - (int)minUsedPages { return minUsedPages; }
    223 - (int)maxUsedPages { return maxUsedPages; }
    224 - (int)length { return bufLen; }
    225 
    226 //
    227 // Žw’肵‚½ˆÊ’u‚Ì“Œvî•ñ‚𓾂é
    228 //   index: ˆÊ’u(ÅV‚ª0)
    229 //
    230 - (const StatisticsRecord*)indexAt:(unsigned)index;
    231 {
    232     if (index >= bufLen) return nil;
    233     index = (index <= bufHead) ? bufHead - index : bufLen + bufHead - index;
    234     return &ringBuffer[index];
    235 }
    236 
    237 //
    238 // ‚à‚Á‚Æ‚àV‚µ‚¢“Œvî•ñ‚ð•Ô‚·
    239 //
    240 - (const StatisticsRecord*)head
    241 {
    242     return &ringBuffer[bufHead];
    243 }
    244 
    245 //
    246 // ‚à‚Á‚Æ‚àŒÃ‚¢“Œvî•ñ‚ð•Ô‚·
    247 //
    248 - (const StatisticsRecord*)tail;
    249 {
    250     return &ringBuffer[bufTail];
    251 }
    252 
    253 @end
    254 
    255 */
Note: See TracChangeset for help on using the changeset viewer.