Changeset 25


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

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

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/MacFaceFloat/MainForm.cs

    r22 r25  
    3131                private FaceDef _currentFaceDef;
    3232                private Configuration _config;
    33                
    34                 int prevUsage;
    35                 int prevMarkers;
    36                 PerformanceCounter cpuCount;
    37                 PerformanceCounter pageoutCount;
    38                 PerformanceCounter pageinCount;
     33
     34                private int prevPattern;
     35                private int prevMarkers;
     36
     37                private CPUUsageCounter cpuCounter;
     38                private MemoryUsageCounter memoryCounter;
    3939
    4040                // ƒRƒ“ƒXƒgƒ‰ƒNƒ^
     
    4545                        this.MoveAtFormDrag = true;
    4646
    47                         prevUsage = -1;
     47                        prevPattern = -1;
    4848                        prevMarkers = -1;
    4949
    50                         cpuCount = new PerformanceCounter();
    51                         cpuCount.CategoryName = "Processor";
    52                         cpuCount.CounterName  = "% Processor Time";
    53                         cpuCount.InstanceName = "_Total";
    54 
    55                         pageoutCount = new PerformanceCounter();
    56                         pageoutCount.CategoryName = "Memory";
    57                         pageoutCount.CounterName  = "Pages Output/sec";
    58 
    59                         pageinCount = new PerformanceCounter();
    60                         pageinCount.CategoryName = "Memory";
    61                         pageinCount.CounterName  = "Pages Input/sec";
     50                        cpuCounter = new CPUUsageCounter();
     51                        memoryCounter = new MemoryUsageCounter();
    6252
    6353                        _updateTimer = new System.Windows.Forms.Timer();
     
    203193                        _facePath = _currentFaceDef.Path;
    204194                        notifyIcon.Text = "MacFace - " + _currentFaceDef.Title;
    205                         prevUsage = -1;
     195                        prevPattern = -1;
    206196                        prevMarkers = -1;
    207197
     
    214204                public void CountProcessorUsage(object sender, EventArgs e)
    215205                {
    216                         int usage = (int)cpuCount.NextValue();
    217                         int pagein = (int)pageinCount.NextValue();
    218                         int pageout = (int)pageoutCount.NextValue();
    219 
    220                         usage /= 10;
    221                         if (usage > 10) {
    222                                 usage = 10;
    223                         } else if (usage < 0) {
    224                                 usage = 0;
     206                        CPUUsage cpuUsage = cpuCounter.CurrentUsage();
     207                        MemoryUsage memUsage = memoryCounter.CurrentUsage();
     208
     209                        int pattern = cpuUsage.Active / 10;
     210                        if (pattern > 10)
     211                        {
     212                                pattern = 10;
     213                        }
     214                        else if (pattern < 0)
     215                        {
     216                                pattern = 0;
    225217                        }
    226218
    227219                        int markers = FaceDef.MarkerNone;
     220                        int pagein = memUsage.Pagein;
     221                        int pageout = memUsage.Pageout;
    228222                        if (pagein > 0) markers += FaceDef.MarkerPageIn;
    229223                        if (pageout > 0) markers += FaceDef.MarkerPageOut;
    230224
    231                         if (prevUsage != usage || prevMarkers != markers)
     225                        if (prevPattern != pattern || prevMarkers != markers)
    232226                        {
    233227                                Graphics g = this.Graphics;
    234228                                g.Clear(Color.FromArgb(0, 0, 0, 0));
    235                                 _currentFaceDef.DrawPatternImage(g, FaceDef.PatternSuite.Normal, usage, markers);
     229                                _currentFaceDef.DrawPatternImage(g, FaceDef.PatternSuite.Normal, pattern, markers);
    236230                                this.Update();
    237231                        }
    238232                               
    239                         prevUsage = usage;
     233                        prevPattern = pattern;
    240234                        prevMarkers = markers;
    241235                }
  • trunk/MacFaceLibrary/CPUUsage.cs

    r24 r25  
    1111        public class CPUUsage
    1212        {
    13                 public CPUUsage()
     13                private int user;
     14                private int system;
     15                private int idle;
     16
     17                public CPUUsage(int user, int system, int idle)
    1418                {
    15                         //
    16                         // TODO: ƒRƒ“ƒXƒgƒ‰ƒNƒ^ ƒƒWƒbƒN‚ð‚±‚±‚ɒljÁ‚µ‚Ä‚­‚¾‚³‚¢B
    17                         //
     19                        this.user = user;
     20                        this.system = system;
     21                        this.idle = idle;
     22                }
     23
     24                public int User
     25                {
     26                        get { return user; }
     27                }
     28
     29                public int System
     30                {
     31                        get { return user; }
     32                }
     33
     34                public int Active
     35                {
     36                        get { return user + system; }
     37                }
     38
     39                public int Idle
     40                {
     41                        get { return idle; }
    1842                }
    1943        }
  • 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 */
  • trunk/MacFaceLibrary/MemoryUsage.cs

    r24 r25  
    1111        public class MemoryUsage
    1212        {
    13                 public MemoryUsage()
     13                private int available;
     14                private int committed;
     15                private int pagein;
     16                private int pageout;
     17
     18                public MemoryUsage(int available, int committed, int pagein, int pageout)
    1419                {
    15                         //
    16                         // TODO: ƒRƒ“ƒXƒgƒ‰ƒNƒ^ ƒƒWƒbƒN‚ð‚±‚±‚ɒljÁ‚µ‚Ä‚­‚¾‚³‚¢B
    17                         //
     20                        this.available = available;
     21                        this.committed = committed;
     22                        this.pagein = pagein;
     23                        this.pageout = pageout;
     24                }
     25
     26                public int Available
     27                {
     28                        get { return available; }
     29                }
     30
     31                public int Committed
     32                {
     33                        get { return committed; }
     34                }
     35
     36                public int Pagein
     37                {
     38                        get { return pagein; }
     39                }
     40
     41                public int Pageout
     42                {
     43                        get { return pageout; }
    1844                }
    1945        }
  • trunk/MacFaceLibrary/MemoryUsageCounter.cs

    r24 r25  
    33 */
    44using System;
     5using System.Diagnostics;
    56
    67namespace MacFace
     
    1112        public class MemoryUsageCounter
    1213        {
     14                private PerformanceCounter availableCounter;
     15                private PerformanceCounter committedCounter;
     16                private PerformanceCounter pageoutCounter;
     17                private PerformanceCounter pageinCounter;
     18
    1319                public MemoryUsageCounter()
    1420                {
    15                         //
    16                         // TODO: ƒRƒ“ƒXƒgƒ‰ƒNƒ^ ƒƒWƒbƒN‚ð‚±‚±‚ɒljÁ‚µ‚Ä‚­‚¾‚³‚¢B
    17                         //
     21                        availableCounter = new PerformanceCounter();
     22                        availableCounter.CategoryName = "Memory";
     23                        availableCounter.CounterName = "Available Bytes";
     24
     25                        committedCounter = new PerformanceCounter();
     26                        committedCounter.CategoryName = "Memory";
     27                        committedCounter.CounterName = "Committed Bytes";
     28
     29                        pageoutCounter = new PerformanceCounter();
     30                        pageoutCounter.CategoryName = "Memory";
     31                        pageoutCounter.CounterName = "Pages Output/sec";
     32
     33                        pageinCounter = new PerformanceCounter();
     34                        pageinCounter.CategoryName = "Memory";
     35                        pageinCounter.CounterName = "Pages Input/sec";
     36                }
     37
     38                public MemoryUsage CurrentUsage()
     39                {
     40                        int available = (int)availableCounter.NextValue();
     41                        int committed = (int)committedCounter.NextValue();
     42                        int pagein      = (int)pageinCounter.NextValue();
     43                        int pageout   = (int)pageoutCounter.NextValue();
     44
     45                        return new MemoryUsage(available, committed, pagein, pageout);
    1846                }
    1947        }
Note: See TracChangeset for help on using the changeset viewer.