2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-17 18:56:15 -05:00
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2016-04-15 09:07:24 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-04-08 07:22:22 -05:00
|
|
|
#include <execinfo.h>
|
2016-08-30 02:06:39 -05:00
|
|
|
#include <csignal>
|
2015-12-13 11:04:45 -06:00
|
|
|
#include <sys/poll.h>
|
2015-12-30 10:41:41 -06:00
|
|
|
#include <sys/prctl.h>
|
2016-09-29 09:47:28 -05:00
|
|
|
#include <sys/stat.h>
|
2016-04-08 07:22:22 -05:00
|
|
|
#include <sys/uio.h>
|
2016-09-29 09:47:28 -05:00
|
|
|
#include <sys/vfs.h>
|
2016-04-20 08:43:00 -05:00
|
|
|
#include <unistd.h>
|
2015-12-13 11:04:45 -06:00
|
|
|
|
2016-04-20 08:43:00 -05:00
|
|
|
#include <atomic>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <cassert>
|
2016-09-27 07:48:32 -05:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstdio>
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <cstdlib>
|
2015-04-09 17:25:48 -05:00
|
|
|
#include <cstring>
|
2016-09-27 07:48:32 -05:00
|
|
|
#include <fstream>
|
2015-11-06 04:46:31 -06:00
|
|
|
#include <iomanip>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <iostream>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <mutex>
|
|
|
|
#include <random>
|
2015-11-06 04:46:31 -06:00
|
|
|
#include <sstream>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <string>
|
2016-11-13 15:12:01 -06:00
|
|
|
#include <thread>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-05-30 19:42:44 -05:00
|
|
|
#include <Poco/Base64Encoder.h>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <Poco/ConsoleChannel.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-04-27 06:16:37 -05:00
|
|
|
#include <Poco/Format.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Process.h>
|
2016-05-30 19:42:44 -05:00
|
|
|
#include <Poco/RandomStream.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <Poco/TemporaryFile.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Thread.h>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <Poco/Timestamp.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-03-08 01:31:29 -06:00
|
|
|
#include "Common.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-09-28 14:07:07 -05:00
|
|
|
#include "Util.hpp"
|
2015-03-28 06:53:44 -05:00
|
|
|
|
2015-12-19 08:16:44 -06:00
|
|
|
namespace Util
|
|
|
|
{
|
2017-01-13 06:55:01 -06:00
|
|
|
namespace rng
|
2016-05-30 19:42:44 -05:00
|
|
|
{
|
2017-01-13 06:55:01 -06:00
|
|
|
static std::random_device _rd;
|
|
|
|
static std::mutex _rngMutex;
|
|
|
|
static Poco::RandomBuf _randBuf;
|
|
|
|
|
|
|
|
// Create the prng with a random-device for seed.
|
|
|
|
// If we don't have a hardware random-device, we will get the same seed.
|
|
|
|
// In that case we are better off with an arbitrary, but changing, seed.
|
|
|
|
static std::mt19937_64 _rng = std::mt19937_64(_rd.entropy()
|
|
|
|
? _rd()
|
|
|
|
: (clock() + getpid()));
|
|
|
|
|
|
|
|
// A new seed is used to shuffle the sequence.
|
|
|
|
// N.B. Always reseed after getting forked!
|
|
|
|
void reseed()
|
|
|
|
{
|
|
|
|
_rng.seed(_rd.entropy() ? _rd() : (clock() + getpid()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new random number.
|
|
|
|
unsigned getNext()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_rngMutex);
|
|
|
|
return _rng();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char> getBytes(const size_t length)
|
|
|
|
{
|
|
|
|
std::vector<char> v(length);
|
|
|
|
_randBuf.readFromDevice(v.data(), v.size());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates a random string in Base64.
|
|
|
|
/// Note: May contain '/' characters.
|
|
|
|
std::string getB64String(const size_t length)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
Poco::Base64Encoder b64(ss);
|
|
|
|
b64.write(getBytes(length).data(), length);
|
|
|
|
return ss.str().substr(0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getFilename(const size_t length)
|
|
|
|
{
|
|
|
|
std::string s = getB64String(length);
|
|
|
|
std::replace(s.begin(), s.end(), '/', '_');
|
|
|
|
return s.substr(0, length);
|
|
|
|
}
|
2016-05-30 19:42:44 -05:00
|
|
|
}
|
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
std::string encodeId(const unsigned number, const int padding)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << std::hex << std::setw(padding) << std::setfill('0') << number;
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-01-09 14:46:08 -06:00
|
|
|
unsigned decodeId(const std::string& str)
|
|
|
|
{
|
|
|
|
unsigned id = 0;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex << str;
|
|
|
|
ss >> id;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
bool windowingAvailable()
|
|
|
|
{
|
2015-12-29 19:34:53 -06:00
|
|
|
return std::getenv("DISPLAY") != nullptr;
|
2015-03-28 06:53:44 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
static 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;
|
|
|
|
|
|
|
|
const auto str = std::string(line + len, strlen(line + len) - 1);
|
|
|
|
return line + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<size_t, size_t> getPssAndDirtyFromSMaps(FILE* file)
|
|
|
|
{
|
|
|
|
size_t numPSSKb = 0;
|
|
|
|
size_t numDirtyKb = 0;
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
rewind(file);
|
|
|
|
char line[4096] = { 0 };
|
|
|
|
while (fgets(line, sizeof (line), file))
|
|
|
|
{
|
|
|
|
const char *value;
|
2017-02-07 08:39:56 -06:00
|
|
|
// Shared_Dirty is accounted for by forkit's RSS
|
2017-02-07 14:07:37 -06:00
|
|
|
if ((value = startsWith(line, "Private_Dirty:")))
|
2017-02-03 00:29:53 -06:00
|
|
|
{
|
|
|
|
numDirtyKb += atoi(value);
|
|
|
|
}
|
|
|
|
else if ((value = startsWith(line, "Pss:")))
|
|
|
|
{
|
|
|
|
numPSSKb += atoi(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(numPSSKb, numDirtyKb);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getMemoryStats(FILE* file)
|
|
|
|
{
|
|
|
|
const auto pssAndDirtyKb = getPssAndDirtyFromSMaps(file);
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "procmemstats: pid=" << getpid()
|
|
|
|
<< " pss=" << pssAndDirtyKb.first
|
|
|
|
<< " dirty=" << pssAndDirtyKb.second;
|
|
|
|
LOG_TRC("Collected " << oss.str());
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getMemoryUsagePSS(const Poco::Process::PID pid)
|
|
|
|
{
|
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
const auto cmd = "/proc/" + std::to_string(pid) + "/smaps";
|
|
|
|
FILE* fp = fopen(cmd.c_str(), "r");
|
|
|
|
if (fp != nullptr)
|
|
|
|
{
|
|
|
|
const auto pss = getPssAndDirtyFromSMaps(fp).first;
|
|
|
|
fclose(fp);
|
|
|
|
return pss;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getMemoryUsageRSS(const Poco::Process::PID pid)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2017-02-08 22:30:28 -06:00
|
|
|
static const auto pageSizeBytes = getpagesize();
|
2017-01-15 12:32:07 -06:00
|
|
|
|
2017-02-08 22:30:28 -06:00
|
|
|
if (pid > 0)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2017-02-08 22:30:28 -06:00
|
|
|
const auto cmd = "/proc/" + std::to_string(pid) + "/stat";
|
|
|
|
FILE* fp = fopen(cmd.c_str(), "r");
|
|
|
|
if (fp != nullptr)
|
2016-11-09 20:12:09 -06:00
|
|
|
{
|
2017-02-08 22:30:28 -06:00
|
|
|
size_t rss = 0;
|
|
|
|
char line[4096] = { 0 };
|
|
|
|
if (fgets(line, sizeof (line), fp))
|
|
|
|
{
|
|
|
|
const std::string s(line);
|
|
|
|
int index = 1;
|
|
|
|
auto pos = s.find(' ');
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
if (index == 23)
|
|
|
|
{
|
|
|
|
// Convert from memory pages to KB.
|
|
|
|
rss = strtol(&s[pos], nullptr, 10);
|
|
|
|
rss *= pageSizeBytes;
|
|
|
|
rss /= 1024;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++index;
|
|
|
|
pos = s.find(' ', pos + 1);
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 12:49:01 -06:00
|
|
|
|
2017-02-08 22:30:28 -06:00
|
|
|
fclose(fp);
|
|
|
|
return rss;
|
2016-11-09 20:12:09 -06:00
|
|
|
}
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
|
|
|
|
2017-01-15 12:32:07 -06:00
|
|
|
return 0;
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
2016-03-31 03:01:21 -05:00
|
|
|
|
|
|
|
std::string replace(const std::string& s, const std::string& a, const std::string& b)
|
|
|
|
{
|
|
|
|
std::string result = s;
|
|
|
|
std::string::size_type pos;
|
|
|
|
while ((pos = result.find(a)) != std::string::npos)
|
|
|
|
{
|
|
|
|
result = result.replace(pos, a.size(), b);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string formatLinesForLog(const std::string& s)
|
|
|
|
{
|
|
|
|
std::string r;
|
|
|
|
std::string::size_type n = s.size();
|
|
|
|
if (n > 0 && s.back() == '\n')
|
|
|
|
r = s.substr(0, n-1);
|
|
|
|
else
|
|
|
|
r = s;
|
|
|
|
return replace(r, "\n", " / ");
|
|
|
|
}
|
2016-04-07 02:55:57 -05:00
|
|
|
|
|
|
|
void setThreadName(const std::string& s)
|
|
|
|
{
|
|
|
|
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(s.c_str()), 0, 0, 0) != 0)
|
2016-11-13 10:59:34 -06:00
|
|
|
{
|
|
|
|
LOG_SYS("Cannot set thread name to " << s << ".");
|
|
|
|
}
|
2016-04-07 02:55:57 -05:00
|
|
|
}
|
2016-04-15 09:07:24 -05:00
|
|
|
|
2016-06-20 04:51:35 -05:00
|
|
|
void getVersionInfo(std::string& version, std::string& hash)
|
2016-04-15 09:07:24 -05:00
|
|
|
{
|
2016-06-20 04:51:35 -05:00
|
|
|
version = std::string(LOOLWSD_VERSION);
|
|
|
|
hash = std::string(LOOLWSD_VERSION_HASH);
|
2016-04-15 09:07:24 -05:00
|
|
|
hash.resize(std::min(8, (int)hash.length()));
|
|
|
|
}
|
2016-04-20 08:43:00 -05:00
|
|
|
|
|
|
|
std::string UniqueId()
|
|
|
|
{
|
|
|
|
static std::atomic_int counter(0);
|
|
|
|
return std::to_string(Poco::Process::id()) + "/" + std::to_string(counter++);
|
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|