Changeset 10534 in vbox for trunk/src/VBox/Runtime/r3/posix
- Timestamp:
- Jul 11, 2008 3:21:53 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/posix/system-posix.cpp
r10528 r10534 92 92 } 93 93 94 /** 95 * Gets the current figures of overall system processor usage. 96 * 97 * @remarks To get meaningful stats this function has to be 98 * called twice with a bit of delay between calls. This 99 * is due to the fact that at least two samples of 100 * system usage stats are needed to calculate the load. 101 * 102 * @returns None. 103 */ 104 RTDECL(int) RTSystemProcessorGetUsageStats(PRTCPUUSAGESTATS pStats) 105 { 106 /** @todo r=bird: This is Linux specific and doesn't belong here. Move this to r3/linux/RTSystemGetCpuLoadStats-linux.cpp. */ 107 int rc = VINF_SUCCESS; 108 uint32_t u32UserNow, u32NiceNow, u32SystemNow, u32IdleNow; 109 uint32_t u32UserDelta, u32SystemDelta, u32IdleDelta, u32BusyDelta, u32TotalDelta; 110 FILE *f = fopen("/proc/stat", "r"); 111 112 if (f) 113 { 114 if (fscanf(f, "cpu %u %u %u %u", &u32UserNow, &u32NiceNow, &u32SystemNow, &u32IdleNow) == 4) 115 { 116 u32UserDelta = (u32UserNow - pStats->u32RawUser) + (u32NiceNow - pStats->u32RawNice); 117 u32SystemDelta = u32SystemNow - pStats->u32RawSystem; 118 u32IdleDelta = u32IdleNow - pStats->u32RawIdle; 119 u32BusyDelta = u32UserDelta + u32SystemDelta; 120 u32TotalDelta = u32BusyDelta + u32IdleDelta; 121 pStats->u32User = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32UserDelta / u32TotalDelta); 122 pStats->u32System = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32SystemDelta / u32TotalDelta); 123 pStats->u32Idle = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32IdleDelta / u32TotalDelta); 124 /* Update the base. */ 125 pStats->u32RawUser = u32UserNow; 126 pStats->u32RawNice = u32NiceNow; 127 pStats->u32RawSystem = u32SystemNow; 128 pStats->u32RawIdle = u32IdleNow; 129 } 130 else 131 rc = VERR_FILE_IO_ERROR; 132 fclose(f); 133 } 134 else 135 rc = VERR_ACCESS_DENIED; 136 137 return rc; 138 } 139 140 /** 141 * Gets the current processor usage for a partucilar process. 142 * 143 * @remarks To get meaningful stats this function has to be 144 * called twice with a bit of delay between calls. This 145 * is due to the fact that at least two samples of 146 * system usage stats are needed to calculate the load. 147 * 148 * @returns None. 149 */ 150 RTDECL(int) RTProcessGetProcessorUsageStats(RTPROCESS pid, PRTPROCCPUUSAGESTATS pStats) 151 { 152 int rc = VINF_SUCCESS; 153 uint32_t u32UserNow, u32NiceNow, u32SystemNow, u32IdleNow; 154 uint32_t u32UserDelta, u32SystemDelta; 155 uint64_t u64TotalNow, u64TotalDelta; 156 157 /** @todo r=bird: This is Linux specific and doesn't belong here. Move this to r3/linux/RTSystemGetCpuLoadStats-linux.cpp. */ 158 FILE *f = fopen("/proc/stat", "r"); 159 160 if (f) 161 { 162 if (fscanf(f, "cpu %u %u %u %u", &u32UserNow, &u32NiceNow, &u32SystemNow, &u32IdleNow) == 4) /** @todo 'uint32_t' is not necessarily the same as 'unsigned int'. */ 163 { 164 char *pszName; 165 pid_t pid2; 166 char c; 167 int iTmp; 168 unsigned uTmp; 169 unsigned long ulTmp, ulUserNow, ulSystemNow; 170 char buf[80]; /* @todo: this should be tied to max allowed proc name. */ 171 172 u64TotalNow = (uint64_t)u32UserNow + u32NiceNow + u32SystemNow + u32IdleNow; 173 fclose(f); 174 RTStrAPrintf(&pszName, "/proc/%d/stat", pid); 175 //printf("Opening %s...\n", pszName); 176 f = fopen(pszName, "r"); 177 RTMemFree(pszName); 178 179 if (f) 180 { 181 if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", 182 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp, 183 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulUserNow, &ulSystemNow) == 15) 184 { 185 Assert((pid_t)pid == pid2); 186 u32UserDelta = ulUserNow - pStats->u32RawProcUser; 187 u32SystemDelta = ulSystemNow - pStats->u32RawProcSystem; 188 u64TotalDelta = u64TotalNow - pStats->u64RawTotal; 189 pStats->u32User = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32UserDelta / u64TotalDelta); 190 pStats->u32System = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32SystemDelta / u64TotalDelta); 191 // printf("%d: user=%u%% system=%u%% / raw user=%u raw system=%u total delta=%u\n", pid, 192 // pStats->u32User / 10000000, pStats->u32System / 10000000, 193 // pStats->u32RawProcUser, pStats->u32RawProcSystem, u64TotalDelta); 194 /* Update the base. */ 195 pStats->u32RawProcUser = ulUserNow; 196 pStats->u32RawProcSystem = ulSystemNow; 197 pStats->u64RawTotal = u64TotalNow; 198 // printf("%d: updated raw user=%u raw system=%u raw total=%u\n", pid, 199 // pStats->u32RawProcUser, pStats->u32RawProcSystem, pStats->u64RawTotal); 200 } 201 else 202 rc = VERR_FILE_IO_ERROR; 203 } 204 else 205 rc = VERR_ACCESS_DENIED; 206 } 207 else 208 rc = VERR_FILE_IO_ERROR; 209 fclose(f); 210 } 211 else 212 rc = VERR_ACCESS_DENIED; 213 214 return rc; 215 }
Note:
See TracChangeset
for help on using the changeset viewer.