1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*
* Phusion Passenger - https://www.phusionpassenger.com/
* Copyright (c) 2010-2013 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_PROCESS_METRICS_COLLECTOR_H_
#define _PASSENGER_PROCESS_METRICS_COLLECTOR_H_
#include <boost/cstdint.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <oxt/system_calls.hpp>
#include <string>
#include <vector>
#include <map>
#ifdef __APPLE__
#include <mach/mach_traps.h>
#include <mach/mach_init.h>
#include <mach/mach_vm.h>
#include <mach/mach_port.h>
#endif
#if !defined(__NetBSD__) && !defined(__OpenBSD__)
// NetBSD does not support -p with multiple PIDs.
// https://code.google.com/p/phusion-passenger/issues/detail?id=736
// OpenBSD 5.2 doesn't support it either
#define PS_SUPPORTS_MULTIPLE_PIDS
#include <set>
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <signal.h>
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <StaticString.h>
#include <Exceptions.h>
#include <Utils.h>
#include <Utils/ScopeGuard.h>
#include <Utils/IOUtils.h>
#include <Utils/StringScanning.h>
namespace Passenger {
using namespace boost;
using namespace std;
using namespace oxt;
/** All sizes are in KB. */
struct ProcessMetrics {
pid_t pid;
pid_t ppid;
boost::uint8_t cpu;
/** Resident Set Size, amount of memory in RAM. Does not include swap.
* -1 if not yet known, 0 if completely swapped out.
*/
ssize_t rss;
/** Proportional Set Size, see measureRealMemory(). Does not include swap.
* -1 if unknown, 0 if completely swapped out.
*/
ssize_t pss;
/** Private dirty RSS, see measureRealMemory(). Does not include swap.
* -1 if unknown, 0 if completely swapped out.
*/
ssize_t privateDirty;
/** Amount of memory in swap.
* -1 if unknown, 0 if no swap used.
*/
ssize_t swap;
/** OS X Snow Leopard does not report the VM size correctly, so don't use this. */
ssize_t vmsize;
pid_t processGroupId;
uid_t uid;
string command;
ProcessMetrics() {
pid = (pid_t) -1;
rss = -1;
pss = -1;
privateDirty = -1;
swap = -1;
vmsize = -1;
}
bool isValid() const {
return pid != (pid_t) -1;
}
/**
* Returns an estimate of the "real" memory usage of a process in KB.
* We don't use the PSS here because that would mean if another
* process that shares memory quits, this process's memory usage
* would suddenly go up.
*/
size_t realMemory() const {
ssize_t swap;
if (this->swap != -1) {
swap = this->swap;
} else {
swap = 0;
}
if (privateDirty != -1) {
return privateDirty + swap;
} else if (rss != -1) {
return rss + swap;
} else {
return 0;
}
}
};
class ProcessMetricMap: public map<pid_t, ProcessMetrics> {
public:
/**
* Returns the total memory usage of all processes in KB, possibly
* including shared memory.
* If measurable, the return value only includes the processes' private
* memory usage (swap is accounted for), and <em>shared</em> is set to the
* amount of shared memory.
* If not measurable, then the return value is an estimate of the total
* memory usage of all processes (which may or may not include shared memory
* as well), and <em>shared</em> is set to -1.
*/
size_t totalMemory(ssize_t &shared) const {
const_iterator it, end = this->end();
bool pssAndPrivateDirtyAvailable = true;
for (it = begin(); it != end && pssAndPrivateDirtyAvailable; it++) {
const ProcessMetrics &metric = it->second;
pssAndPrivateDirtyAvailable = pssAndPrivateDirtyAvailable &&
metric.pss != -1 && metric.privateDirty != -1;
}
if (pssAndPrivateDirtyAvailable) {
size_t total = 0;
size_t priv = 0;
for (it = begin(); it != end; it++) {
const ProcessMetrics &metric = it->second;
total += metric.pss;
priv += metric.privateDirty;
}
shared = total - priv;
return total;
} else {
size_t total = 0;
for (it = begin(); it != end; it++) {
const ProcessMetrics &metric = it->second;
total += metric.realMemory();
}
shared = -1;
return total;
}
}
};
/**
* Utility class for collection metrics on processes, such as CPU usage, memory usage,
* command name, etc.
*/
class ProcessMetricsCollector {
private:
bool canMeasureRealMemory;
string psOutput;
template<typename Collection, typename ConstIterator>
ProcessMetricMap parsePsOutput(const string &output, const Collection &allowedPids) const {
ProcessMetricMap result;
const char *start = output.c_str();
// Ignore first line, it contains the column names.
if (!skipToNextLine(&start) || *start == '\0') {
start = NULL;
}
#ifndef PS_SUPPORTS_MULTIPLE_PIDS
set<pid_t> pids;
ConstIterator it, end = allowedPids.end();
for (it = allowedPids.begin(); it != allowedPids.end(); it++) {
pids.insert(*it);
}
#endif
// Parse each line.
while (start != NULL) {
ProcessMetrics metrics;
metrics.pid = (pid_t) readNextWordAsLongLong(&start);
metrics.ppid = (pid_t) readNextWordAsLongLong(&start);
metrics.cpu = readNextWordAsInt(&start);
metrics.rss = (size_t) readNextWordAsLongLong(&start);
metrics.vmsize = (size_t) readNextWordAsLongLong(&start);
metrics.processGroupId = (pid_t) readNextWordAsLongLong(&start);
metrics.uid = (uid_t) readNextWordAsLongLong(&start);
metrics.command = readRestOfLine(start);
bool pidAllowed;
#ifdef PS_SUPPORTS_MULTIPLE_PIDS
pidAllowed = true;
#else
pidAllowed = pids.find(metrics.pid) != pids.end();
#endif
if (pidAllowed) {
result[metrics.pid] = metrics;
start = strchr(start, '\n');
if (start != NULL) {
// Skip to beginning of next line.
start++;
if (*start == '\0') {
start = NULL;
}
}
}
}
return result;
}
public:
ProcessMetricsCollector() {
#ifdef __APPLE__
canMeasureRealMemory = true;
#else
canMeasureRealMemory = fileExists("/proc/self/smaps");
#endif
}
/** Mock 'ps' output, used by unit tests. */
void setPsOutput(const string &data) {
this->psOutput = data;
}
/**
* Collect metrics for the given process IDs. Nonexistant PIDs are not
* included in the result.
*
* Returns a map which maps a given PID to its collected metrics.
*
* @throws ParseException The ps output cannot be parsed.
* @throws SystemException
* @throws RuntimeException
*/
template<typename Collection, typename ConstIterator>
ProcessMetricMap collect(const Collection &pids) const {
if (pids.empty()) {
return ProcessMetricMap();
}
ConstIterator it;
// The list of PIDs must follow -p without a space.
// https://groups.google.com/forum/#!topic/phusion-passenger/WKXy61nJBMA
string pidsArg = "-p";
for (it = pids.begin(); it != pids.end(); it++) {
pidsArg.append(toString(*it));
pidsArg.append(",");
}
if (pidsArg[pidsArg.size() - 1] == ',') {
pidsArg.resize(pidsArg.size() - 1);
}
// The list of format arguments must also follow -o
// without a space.
// https://github.com/phusion/passenger/pull/94
string fmtArg = "-o";
#if defined(sun) || defined(__sun)
fmtArg.append("pid,ppid,pcpu,rss,vsz,pgid,uid,args");
#else
fmtArg.append("pid,ppid,%cpu,rss,vsize,pgid,uid,command");
#endif
const char *command[] = {
"ps", fmtArg.c_str(),
#ifdef PS_SUPPORTS_MULTIPLE_PIDS
pidsArg.c_str(),
#endif
NULL
};
string psOutput = this->psOutput;
if (psOutput.empty()) {
psOutput = runCommandAndCaptureOutput(command);
}
pidsArg.resize(0);
fmtArg.resize(0);
ProcessMetricMap result = parsePsOutput<Collection, ConstIterator>(psOutput, pids);
psOutput.resize(0);
if (canMeasureRealMemory) {
ProcessMetricMap::iterator it;
for (it = result.begin(); it != result.end(); it++) {
ProcessMetrics &metric = it->second;
measureRealMemory(metric.pid, metric.pss,
metric.privateDirty, metric.swap);
}
}
return result;
}
ProcessMetricMap collect(const vector<pid_t> &pids) const {
return collect< vector<pid_t>, vector<pid_t>::const_iterator >(pids);
}
/**
* Attempt to measure various parts of a process's memory usage that may
* contribute to insight as to what its "real" memory usage might be.
* Collected information are:
* - The proportional set size: total size of a process's pages that are in
* memory, where the size of each page is divided by the number of processes
* sharing it.
* - The private dirty RSS.
* - Amount of memory in swap.
*
* At this time only OS X and recent Linux versions (>= 2.6.25) support
* measuring the proportional set size. Usually root privileges are required.
*
* pss, privateDirty and swap can each be individually set to -1 if that
* part cannot be measured, e.g. because we do not have permission
* to do so or because the OS does not support measuring it.
*/
static void measureRealMemory(pid_t pid, ssize_t &pss, ssize_t &privateDirty, ssize_t &swap) {
#ifdef __APPLE__
kern_return_t ret;
mach_port_t task;
swap = -1;
ret = task_for_pid(mach_task_self(), pid, &task);
if (ret != KERN_SUCCESS) {
pss = -1;
privateDirty = -1;
return;
}
mach_vm_address_t addr = 0;
int pagesize = getpagesize();
// In bytes.
pss = 0;
privateDirty = 0;
while (true) {
mach_vm_address_t size;
vm_region_top_info_data_t info;
mach_msg_type_number_t count = VM_REGION_TOP_INFO_COUNT;
mach_port_t object_name;
ret = mach_vm_region(task, &addr, &size, VM_REGION_TOP_INFO,
(vm_region_info_t) &info, &count, &object_name);
if (ret != KERN_SUCCESS) {
break;
}
if (info.share_mode == SM_PRIVATE) {
// shared_pages_resident here means that region
// has shared memory only "shared" between 1 process.
pss += info.private_pages_resident * pagesize;
pss += info.shared_pages_resident * pagesize;
privateDirty += info.private_pages_resident * pagesize;
} else if (info.share_mode == SM_COW) {
pss += info.private_pages_resident * pagesize;
pss += info.shared_pages_resident * pagesize / info.ref_count;
privateDirty += info.private_pages_resident * pagesize;
} else if (info.share_mode == SM_SHARED) {
pss += info.shared_pages_resident * pagesize / info.ref_count;
}
addr += size;
}
mach_port_deallocate(mach_task_self(), task);
// Convert result back to KB.
pss /= 1024;
privateDirty /= 1024;
#else
string smapsFilename = "/proc/";
smapsFilename.append(toString(pid));
smapsFilename.append("/smaps");
FILE *f = syscalls::fopen(smapsFilename.c_str(), "r");
if (f == NULL) {
error:
pss = -1;
privateDirty = -1;
swap = -1;
return;
}
StdioGuard guard(f);
bool hasPss = false;
bool hasPrivateDirty = false;
bool hasSwap = false;
// In KB.
pss = 0;
privateDirty = 0;
swap = 0;
while (!feof(f)) {
char line[1024 * 4];
const char *buf;
buf = fgets(line, sizeof(line), f);
if (buf == NULL) {
if (ferror(f)) {
goto error;
} else {
break;
}
}
try {
if (startsWith(line, "Pss:")) {
/* Linux supports Proportional Set Size since kernel 2.6.25.
* See kernel commit ec4dd3eb35759f9fbeb5c1abb01403b2fde64cc9.
*/
hasPss = true;
readNextWord(&buf);
pss += readNextWordAsLongLong(&buf);
if (readNextWord(&buf) != "kB") {
goto error;
}
} else if (startsWith(line, "Private_Dirty:")) {
hasPrivateDirty = true;
readNextWord(&buf);
privateDirty += readNextWordAsLongLong(&buf);
if (readNextWord(&buf) != "kB") {
goto error;
}
} else if (startsWith(line, "Swap:")) {
hasSwap = true;
readNextWord(&buf);
swap += readNextWordAsLongLong(&buf);
if (readNextWord(&buf) != "kB") {
goto error;
}
}
} catch (const ParseException &) {
goto error;
}
}
if (!hasPss) {
pss = -1;
}
if (!hasPrivateDirty) {
privateDirty = -1;
}
if (!hasSwap) {
swap = -1;
}
#endif
}
};
} // namespace Passenger
#endif /* _PASSENGER_PROCESS_METRICS_COLLECTOR_H_ */
|