src/agent/Watchdog/WatchdogMain.cpp in passenger-5.0.26 vs src/agent/Watchdog/WatchdogMain.cpp in passenger-5.0.27

- old
+ new

@@ -160,11 +160,11 @@ /***** Functions *****/ static FILE * -openOomAdjFile(const char *mode, OomFileType &type) { +openOomAdjFileGetType(const char *mode, OomFileType &type) { FILE *f = fopen("/proc/self/oom_score_adj", mode); if (f == NULL) { f = fopen("/proc/self/oom_adj", mode); if (f == NULL) { return NULL; @@ -176,10 +176,20 @@ type = OOM_SCORE_ADJ; return f; } } +static FILE * +openOomAdjFileForcedType(const char *mode, OomFileType &type) { + if (type == OOM_SCORE_ADJ) { + return fopen("/proc/self/oom_score_adj", mode); + } else { + assert(type == OOM_ADJ); + return fopen("/proc/self/oom_adj", mode); + } +} + /** * Linux-only way to change OOM killer configuration for * current process. Requires root privileges, which we * should have. */ @@ -189,18 +199,28 @@ return; } FILE *f; OomFileType type; + string filteredScore; - f = openOomAdjFile("w", type); + if (score.at(0) == 'l') { + filteredScore = score.substr(1); + type = OOM_ADJ; + } else { + filteredScore = score; + type = OOM_SCORE_ADJ; + } + f = openOomAdjFileForcedType("w", type); if (f != NULL) { - size_t ret = fwrite(score.data(), 1, score.size(), f); + size_t ret = fwrite(filteredScore.data(), 1, filteredScore.size(), f); // We can't do anything about failures, so ignore compiler // warnings about not doing anything with the result. (void) ret; fclose(f); + } else { + P_WARN("setOomScore(" << filteredScore << ", " << type << ") failed due to error: " << strerror(errno)); } } /** * Set the current process's OOM score to "never kill". @@ -209,14 +229,18 @@ setOomScoreNeverKill() { string oldScore; FILE *f; OomFileType type; - f = openOomAdjFile("r", type); + f = openOomAdjFileGetType("r", type); if (f == NULL) { return ""; } + // mark if this is a legacy score so we won't try to write it as OOM_SCORE_ADJ + if (type == OOM_ADJ) { + oldScore.append("l"); + } char buf[1024]; size_t bytesRead; while (true) { bytesRead = fread(buf, 1, sizeof(buf), f); if (bytesRead == 0 && feof(f)) { @@ -228,11 +252,11 @@ oldScore.append(buf, bytesRead); } } fclose(f); - f = openOomAdjFile("w", type); + f = openOomAdjFileForcedType("w", type); if (f == NULL) { return ""; } if (type == OOM_SCORE_ADJ) { fprintf(f, "-1000\n"); @@ -395,11 +419,11 @@ pid = fork(); if (pid == 0) { // Child try { vector<AgentWatcherPtr>::const_iterator it; - Timer timer(false); + Timer<SystemTime::GRAN_10MSEC> timer(false); fd_set fds, fds2; int max, agentProcessesDone; unsigned long long deadline = 30000; // miliseconds #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(sun) @@ -779,9 +803,10 @@ oldOomScore = setOomScoreNeverKill(); agentsOptions = new VariantMap(); *agentsOptions = initializeAgent(argc, &argv, SHORT_PROGRAM_NAME " watchdog", parseOptions, NULL, 2); + agentsOptions->set("original_oom_score", oldOomScore); // Start all sub-agents with this environment variable. setenv("PASSENGER_USE_FEEDBACK_FD", "true", 1); wo = boost::make_shared<WorkingObjects>();