| 4143 | } |
| 4144 | |
| 4145 | bool SystemInformationImplementation::QueryLinuxMemory() |
| 4146 | { |
| 4147 | #if defined(__linux) |
| 4148 | unsigned long tv = 0; |
| 4149 | unsigned long tp = 0; |
| 4150 | unsigned long av = 0; |
| 4151 | unsigned long ap = 0; |
| 4152 | |
| 4153 | char buffer[1024]; // for reading lines |
| 4154 | |
| 4155 | int linuxMajor = 0; |
| 4156 | int linuxMinor = 0; |
| 4157 | |
| 4158 | // Find the Linux kernel version first |
| 4159 | struct utsname unameInfo; |
| 4160 | int errorFlag = uname(&unameInfo); |
| 4161 | if (errorFlag != 0) { |
| 4162 | std::cerr << "Problem calling uname(): " << strerror(errno) << "\n"; |
| 4163 | return false; |
| 4164 | } |
| 4165 | |
| 4166 | if (strlen(unameInfo.release) >= 3) { |
| 4167 | // release looks like "2.6.3-15mdk-i686-up-4GB" |
| 4168 | char majorChar = unameInfo.release[0]; |
| 4169 | char minorChar = unameInfo.release[2]; |
| 4170 | |
| 4171 | if (kwsysString_isdigit(majorChar)) { |
| 4172 | linuxMajor = majorChar - '0'; |
| 4173 | } |
| 4174 | |
| 4175 | if (kwsysString_isdigit(minorChar)) { |
| 4176 | linuxMinor = minorChar - '0'; |
| 4177 | } |
| 4178 | } |
| 4179 | |
| 4180 | FILE* fd = fopen("/proc/meminfo", "r"); |
| 4181 | if (!fd) { |
| 4182 | std::cerr << "Problem opening /proc/meminfo\n"; |
| 4183 | return false; |
| 4184 | } |
| 4185 | |
| 4186 | if (linuxMajor >= 3 || ((linuxMajor >= 2) && (linuxMinor >= 6))) { |
| 4187 | // new /proc/meminfo format since kernel 2.6.x |
| 4188 | // Rigorously, this test should check from the developing version 2.5.x |
| 4189 | // that introduced the new format... |
| 4190 | |
| 4191 | enum |
| 4192 | { |
| 4193 | mMemTotal, |
| 4194 | mMemFree, |
| 4195 | mBuffers, |
| 4196 | mCached, |
| 4197 | mSwapTotal, |
| 4198 | mSwapFree |
| 4199 | }; |
| 4200 | char const* format[6] = { "MemTotal:%lu kB", "MemFree:%lu kB", |
| 4201 | "Buffers:%lu kB", "Cached:%lu kB", |
| 4202 | "SwapTotal:%lu kB", "SwapFree:%lu kB" }; |
no test coverage detected