2016-04-05 11:41:10 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <ftw.h>
|
|
|
|
#include <cassert>
|
2016-04-07 14:15:18 -05:00
|
|
|
#include <iostream>
|
2016-04-25 15:09:17 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2016-04-07 14:15:18 -05:00
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
#include "Util.hpp"
|
|
|
|
#include "Unit.hpp"
|
2016-04-12 15:30:06 -05:00
|
|
|
#include "LOOLProtocol.hpp"
|
2016-04-05 11:41:10 -05:00
|
|
|
|
2016-04-07 14:15:18 -05:00
|
|
|
#include <Poco/Timestamp.h>
|
2016-04-12 15:30:06 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
|
|
|
const int NumToPrefork = 20;
|
2016-04-07 14:15:18 -05:00
|
|
|
|
2016-04-09 12:26:33 -05:00
|
|
|
// Inside the WSD process
|
2016-04-09 11:30:48 -05:00
|
|
|
class UnitPrefork : public UnitWSD
|
2016-04-06 13:50:55 -05:00
|
|
|
{
|
|
|
|
int _numStarted;
|
2016-04-25 15:09:17 -05:00
|
|
|
std::string _failure;
|
2016-04-12 15:30:06 -05:00
|
|
|
Poco::Timestamp _startTime;
|
|
|
|
std::vector< std::shared_ptr<Poco::Net::WebSocket> > _childSockets;
|
2016-04-10 21:29:49 -05:00
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
public:
|
|
|
|
UnitPrefork()
|
2016-04-12 15:30:06 -05:00
|
|
|
: _numStarted(0)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2016-04-09 12:26:33 -05:00
|
|
|
setHasKitHooks();
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
2016-04-10 21:29:49 -05:00
|
|
|
|
2016-04-06 13:50:55 -05:00
|
|
|
virtual void preSpawnCount(int &numPrefork) override
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2016-04-12 15:30:06 -05:00
|
|
|
numPrefork = NumToPrefork;
|
|
|
|
}
|
|
|
|
|
2016-04-13 04:32:01 -05:00
|
|
|
void getMemory(const std::shared_ptr<Poco::Net::WebSocket> &socket,
|
|
|
|
size_t &totalPSS, size_t &totalDirty)
|
2016-04-12 15:30:06 -05:00
|
|
|
{
|
|
|
|
/// Fetch memory usage data from the last process ...
|
|
|
|
socket->sendFrame("unit-memdump: \n", sizeof("unit-memdump: \n")-1);
|
|
|
|
int flags;
|
|
|
|
char buffer[4096];
|
|
|
|
|
|
|
|
int length = socket->receiveFrame(buffer, sizeof (buffer), flags);
|
|
|
|
std::string memory = LOOLProtocol::getFirstLine(buffer, length);
|
|
|
|
|
2016-04-25 15:09:17 -05:00
|
|
|
if (!memory.compare(0,6,"Error:"))
|
|
|
|
_failure = memory;
|
|
|
|
else
|
|
|
|
{
|
2016-04-13 04:32:01 -05:00
|
|
|
// std::cout << "Got memory stats '" << memory << "'" << std::endl;
|
2016-04-25 15:09:17 -05:00
|
|
|
Poco::StringTokenizer tokens(memory, " ");
|
|
|
|
assert (tokens.count() == 2);
|
|
|
|
totalPSS += atoi(tokens[0].c_str());
|
|
|
|
totalDirty += atoi(tokens[1].c_str());
|
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
2016-04-10 21:29:49 -05:00
|
|
|
|
2016-04-12 15:30:06 -05:00
|
|
|
virtual void newChild(const std::shared_ptr<Poco::Net::WebSocket> &socket) override
|
2016-04-06 13:50:55 -05:00
|
|
|
{
|
2016-04-10 21:29:49 -05:00
|
|
|
++_numStarted;
|
2016-04-12 15:30:06 -05:00
|
|
|
_childSockets.push_back(socket);
|
|
|
|
if (_numStarted >= NumToPrefork)
|
2016-04-07 14:15:18 -05:00
|
|
|
{
|
|
|
|
Poco::Timestamp::TimeDiff elapsed = _startTime.elapsed();
|
|
|
|
|
|
|
|
std::cout << "Launched " << _numStarted << " in "
|
|
|
|
<< (1.0 * elapsed)/Poco::Timestamp::resolution() << std::endl;
|
2016-04-13 04:32:01 -05:00
|
|
|
size_t totalPSSKb = 0;
|
|
|
|
size_t totalDirtyKb = 0;
|
2016-04-12 15:30:06 -05:00
|
|
|
for (auto child : _childSockets)
|
2016-04-13 04:32:01 -05:00
|
|
|
getMemory(child, totalPSSKb, totalDirtyKb);
|
|
|
|
|
|
|
|
std::cout << "Memory use total " << totalPSSKb << "k shared "
|
|
|
|
<< totalDirtyKb << "k dirty" << std::endl;
|
|
|
|
|
|
|
|
totalPSSKb /= _childSockets.size();
|
|
|
|
totalDirtyKb /= _childSockets.size();
|
|
|
|
std::cout << "Memory use average " << totalPSSKb << "k shared "
|
|
|
|
<< totalDirtyKb << "k dirty" << std::endl;
|
2016-04-12 15:30:06 -05:00
|
|
|
|
2016-04-25 15:09:17 -05:00
|
|
|
if (!_failure.empty())
|
|
|
|
exitTest(TestResult::TEST_FAILED);
|
|
|
|
else
|
|
|
|
exitTest(TestResult::TEST_OK);
|
2016-04-07 14:15:18 -05:00
|
|
|
}
|
2016-04-06 13:50:55 -05:00
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
};
|
|
|
|
|
2016-04-12 15:30:06 -05:00
|
|
|
namespace {
|
|
|
|
std::vector<int> pids;
|
|
|
|
|
|
|
|
const char *startsWith(const char *line, const char *tag)
|
|
|
|
{
|
|
|
|
int len = strlen(tag);
|
|
|
|
if (!strncmp(line, tag, len))
|
|
|
|
{
|
|
|
|
while (!isdigit(line[len]) && line[len] != '\0')
|
|
|
|
len++;
|
|
|
|
// fprintf(stdout, "does start with %s: '%s'\n", tag, line + len);
|
|
|
|
return line + len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string readMemorySizes(FILE *inStream)
|
|
|
|
{
|
|
|
|
size_t numPSSKb = 0;
|
|
|
|
size_t numDirtyKb = 0;
|
|
|
|
|
|
|
|
char line[4096];
|
|
|
|
while (fgets(line, sizeof (line), inStream))
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
if ((value = startsWith(line, "Private_Dirty:")) ||
|
|
|
|
(value = startsWith(line, "Shared_Dirty:")))
|
|
|
|
numDirtyKb += atoi(value);
|
|
|
|
else if ((value = startsWith(line, "Pss:")))
|
|
|
|
numPSSKb += atoi(value);
|
|
|
|
}
|
|
|
|
std::ostringstream oss;
|
2016-04-13 04:32:01 -05:00
|
|
|
oss << numPSSKb << " " << numDirtyKb;
|
2016-04-12 15:30:06 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 12:26:33 -05:00
|
|
|
// Inside the forkit & kit processes
|
|
|
|
class UnitKitPrefork : public UnitKit
|
|
|
|
{
|
2016-04-12 15:30:06 -05:00
|
|
|
FILE *_procSMaps;
|
2016-04-25 15:09:17 -05:00
|
|
|
std::string _failure;
|
2016-04-12 15:30:06 -05:00
|
|
|
|
2016-04-09 12:26:33 -05:00
|
|
|
public:
|
2016-04-12 15:30:06 -05:00
|
|
|
UnitKitPrefork()
|
|
|
|
: _procSMaps(NULL)
|
|
|
|
{
|
|
|
|
std::cerr << "UnitKit Prefork init !\n";
|
|
|
|
}
|
|
|
|
~UnitKitPrefork()
|
|
|
|
{
|
2016-04-30 12:15:41 -05:00
|
|
|
if (_procSMaps)
|
|
|
|
fclose(_procSMaps);
|
2016-04-12 15:30:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void launchedKit(int pid) override
|
|
|
|
{
|
|
|
|
// by the magic of forking - this should appear
|
|
|
|
// in the last kit child nearly fully formed.
|
|
|
|
pids.push_back(pid);
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:09:17 -05:00
|
|
|
// Check that we have no unexpected open sockets.
|
|
|
|
void checkSockets()
|
|
|
|
{
|
|
|
|
DIR *fds = opendir ("/proc/self/fd");
|
|
|
|
struct dirent *ent;
|
|
|
|
int deviceCount = 0, rdbCount = 0, resCount = 0,
|
|
|
|
numSockets = 0, numUnexpected = 0, pipeCount = 0;
|
|
|
|
while ((ent = readdir(fds)))
|
|
|
|
{
|
|
|
|
if (ent->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
char name[1024 + 32];
|
|
|
|
char buffer[4096];
|
|
|
|
strcpy (name, "/proc/self/fd/");
|
|
|
|
strncat(name, ent->d_name, 1024);
|
|
|
|
size_t len;
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
if ((len = readlink(name, buffer, sizeof(buffer)-1) > 0))
|
|
|
|
{
|
|
|
|
assert(len<sizeof(buffer));
|
|
|
|
numSockets++;
|
|
|
|
char *extDot = strrchr(buffer, '.');
|
|
|
|
// fprintf(stdout, "fd: %s -> %s\n", ent->d_name, buffer);
|
|
|
|
if (!strncmp(buffer, "/dev/", sizeof ("/dev/") -1))
|
|
|
|
deviceCount++;
|
|
|
|
else if (extDot && !strcmp(extDot, ".res"))
|
|
|
|
resCount++;
|
|
|
|
else if (extDot && !strcmp(extDot, ".rdb"))
|
|
|
|
rdbCount++;
|
|
|
|
else if (strstr(buffer, "unit-prefork.log") || // our log
|
|
|
|
(strstr(buffer, "/proc/") && // our readdir
|
|
|
|
strstr(buffer, "/fd")))
|
|
|
|
; // ignore
|
|
|
|
else if (!strncmp(buffer, "pipe:[", 6))
|
|
|
|
pipeCount++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unexpected descriptor: %s -> %s\n", ent->d_name, buffer);
|
|
|
|
numUnexpected++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%d devices, %d rdb %d resources, %d pipes, %d descriptors total: %d unexpected\n",
|
|
|
|
deviceCount, rdbCount, resCount, pipeCount, numSockets, numUnexpected);
|
|
|
|
if (pipeCount > 2 || numUnexpected > 0)
|
|
|
|
_failure = std::string("Error: unexpected inherited sockets ") +
|
|
|
|
std::to_string(numUnexpected) + " and pipes " +
|
|
|
|
std::to_string(pipeCount);
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:30:06 -05:00
|
|
|
virtual void postFork() override
|
|
|
|
{
|
2016-04-25 15:09:17 -05:00
|
|
|
checkSockets();
|
|
|
|
|
2016-04-12 15:30:06 -05:00
|
|
|
// before we drop the caps we can even open our /proc files !
|
2016-04-17 08:54:05 -05:00
|
|
|
const std::string procName = std::string("/proc/") +
|
|
|
|
std::to_string(getpid()) +
|
|
|
|
std::string("/smaps");
|
2016-04-12 15:30:06 -05:00
|
|
|
_procSMaps = fopen(procName.c_str(), "r");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool filterKitMessage(const std::shared_ptr<Poco::Net::WebSocket> &ws,
|
|
|
|
std::string &message) override
|
|
|
|
{
|
2016-04-17 08:54:05 -05:00
|
|
|
const auto token = LOOLProtocol::getFirstToken(message.c_str(), message.length());
|
2016-04-12 15:30:06 -05:00
|
|
|
if (token == "unit-memdump:")
|
|
|
|
{
|
2016-04-25 15:09:17 -05:00
|
|
|
std::string memory;
|
|
|
|
if (!_failure.empty())
|
|
|
|
memory = _failure;
|
|
|
|
else
|
|
|
|
memory = readMemorySizes(_procSMaps) + "\n";
|
2016-04-12 15:30:06 -05:00
|
|
|
ws->sendFrame(memory.c_str(), memory.length());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-09 12:26:33 -05:00
|
|
|
};
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase *unit_create_wsd(void)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
|
|
|
return new UnitPrefork();
|
|
|
|
}
|
|
|
|
|
2016-04-09 12:26:33 -05:00
|
|
|
UnitBase *unit_create_kit(void)
|
|
|
|
{
|
|
|
|
return new UnitKitPrefork();
|
|
|
|
}
|
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|