2016-04-15 09:01:02 -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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_COUNTLOOLKITPROCESSES_HPP
|
|
|
|
#define INCLUDED_COUNTLOOLKITPROCESSES_HPP
|
|
|
|
|
2017-01-06 09:54:19 -06:00
|
|
|
#include <iostream>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2016-04-28 21:01:05 -05:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2017-01-06 09:54:19 -06:00
|
|
|
#include <Poco/FileStream.h>
|
|
|
|
#include <Poco/StreamCopier.h>
|
2016-04-28 21:01:05 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2017-01-06 09:54:19 -06:00
|
|
|
#include <Common.hpp>
|
|
|
|
|
2016-04-28 21:01:05 -05:00
|
|
|
/// Counts the number of LoolKit process instances without wiating.
|
2016-10-29 20:15:00 -05:00
|
|
|
static int getLoolKitProcessCount()
|
2016-04-28 21:01:05 -05:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
for (auto i = Poco::DirectoryIterator(std::string("/proc")); i != Poco::DirectoryIterator(); ++i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Poco::Path procEntry = i.path();
|
|
|
|
const std::string& fileName = procEntry.getFileName();
|
|
|
|
int pid;
|
|
|
|
std::size_t endPos = 0;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pid = std::stoi(fileName, &endPos);
|
|
|
|
}
|
|
|
|
catch (const std::invalid_argument&)
|
|
|
|
{
|
|
|
|
pid = 0;
|
|
|
|
}
|
2017-01-06 09:54:19 -06:00
|
|
|
|
2016-04-28 21:01:05 -05:00
|
|
|
if (pid > 1 && endPos == fileName.length())
|
|
|
|
{
|
|
|
|
Poco::FileInputStream stat(procEntry.toString() + "/stat");
|
|
|
|
std::string statString;
|
|
|
|
Poco::StreamCopier::copyToString(stat, statString);
|
|
|
|
Poco::StringTokenizer tokens(statString, " ");
|
|
|
|
if (tokens.count() > 3 && tokens[1] == "(loolkit)")
|
|
|
|
{
|
|
|
|
switch (tokens[2].c_str()[0])
|
|
|
|
{
|
2016-11-11 21:23:05 -06:00
|
|
|
// Dead marker for old and new kernels.
|
2016-04-28 21:01:05 -05:00
|
|
|
case 'x':
|
2016-11-11 21:23:05 -06:00
|
|
|
case 'X':
|
|
|
|
// Don't ignore zombies.
|
|
|
|
break;
|
2016-04-28 21:01:05 -05:00
|
|
|
default:
|
2016-11-11 21:23:05 -06:00
|
|
|
++result;
|
2016-04-28 21:01:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// std::cout << "Process:" << pid << ", '" << tokens[1] << "'" << " state: " << tokens[2] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 21:23:05 -06:00
|
|
|
catch (const std::exception& ex)
|
2016-04-28 21:01:05 -05:00
|
|
|
{
|
2016-11-13 20:59:14 -06:00
|
|
|
// 'File not found' is common here, since there is a race
|
|
|
|
// between iterating the /proc directory and opening files,
|
|
|
|
// the process in question might have been gone.
|
|
|
|
//std::cerr << "Error while iterating processes: " << ex.what() << std::endl;
|
2016-04-28 21:01:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
static int countLoolKitProcesses(const int expected)
|
2016-04-28 21:01:05 -05:00
|
|
|
{
|
2017-01-06 09:54:19 -06:00
|
|
|
std::cerr << "Waiting until loolkit processes are exactly " << expected << ". Loolkits: ";
|
2016-10-17 13:44:28 -05:00
|
|
|
|
2016-11-11 21:23:05 -06:00
|
|
|
// This does not need to depend on any constant from Common.hpp.
|
|
|
|
// The shorter the better (the quicker the test runs).
|
2017-01-06 09:54:19 -06:00
|
|
|
const auto sleepMs = 50;
|
2016-10-17 13:44:28 -05:00
|
|
|
|
|
|
|
// This has to cause waiting for at least COMMAND_TIMEOUT_MS. Add one second for safety.
|
|
|
|
const size_t repeat = ((COMMAND_TIMEOUT_MS + 1000) / sleepMs);
|
2016-04-28 21:01:05 -05:00
|
|
|
auto count = getLoolKitProcessCount();
|
|
|
|
for (size_t i = 0; i < repeat; ++i)
|
|
|
|
{
|
2016-10-08 20:40:52 -05:00
|
|
|
std::cerr << count << ' ';
|
2016-04-28 21:01:05 -05:00
|
|
|
if (count == expected)
|
|
|
|
{
|
2016-10-08 20:40:52 -05:00
|
|
|
break;
|
2016-04-28 21:01:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Give polls in the lool processes time to time out etc
|
2017-01-06 09:54:19 -06:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
|
2016-04-28 21:01:05 -05:00
|
|
|
|
2017-01-29 13:04:54 -06:00
|
|
|
const auto newCount = getLoolKitProcessCount();
|
|
|
|
if (count != newCount)
|
|
|
|
{
|
|
|
|
// Allow more time until the number settles.
|
|
|
|
i = 0;
|
|
|
|
count = newCount;
|
|
|
|
}
|
2016-04-28 21:01:05 -05:00
|
|
|
}
|
|
|
|
|
2016-10-08 20:40:52 -05:00
|
|
|
std::cerr << std::endl;
|
2016-04-29 16:16:53 -05:00
|
|
|
if (expected != count)
|
|
|
|
{
|
|
|
|
std::cerr << "Found " << count << " LoKit processes but was expecting " << expected << "." << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:01:05 -05:00
|
|
|
return count;
|
|
|
|
}
|
2016-04-15 09:01:02 -05:00
|
|
|
|
2017-01-06 09:54:19 -06:00
|
|
|
// FIXME: we probably should make this extern
|
|
|
|
// and reuse it. As it stands now, it is per
|
|
|
|
// translation unit, which isn't desirable if
|
|
|
|
// (in the non-ideal event that) it's not 1,
|
|
|
|
// it will cause testNoExtraLoolKitsLeft to
|
|
|
|
// wait unnecessarily and fail.
|
|
|
|
static int InitialLoolKitCount = 1;
|
2017-01-08 20:11:50 -06:00
|
|
|
static std::chrono::steady_clock::time_point TestStartTime;
|
2017-01-06 09:54:19 -06:00
|
|
|
|
|
|
|
static void testCountHowManyLoolkits()
|
|
|
|
{
|
2017-04-09 17:25:31 -05:00
|
|
|
TestStartTime = std::chrono::steady_clock::now();
|
|
|
|
|
2017-01-06 09:54:19 -06:00
|
|
|
InitialLoolKitCount = countLoolKitProcesses(InitialLoolKitCount);
|
|
|
|
CPPUNIT_ASSERT(InitialLoolKitCount > 0);
|
2017-01-08 20:11:50 -06:00
|
|
|
|
|
|
|
TestStartTime = std::chrono::steady_clock::now();
|
2017-01-06 09:54:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testNoExtraLoolKitsLeft()
|
|
|
|
{
|
|
|
|
const auto countNow = countLoolKitProcesses(InitialLoolKitCount);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(InitialLoolKitCount, countNow);
|
2017-01-08 20:11:50 -06:00
|
|
|
|
|
|
|
const auto duration = (std::chrono::steady_clock::now() - TestStartTime);
|
|
|
|
const auto durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
|
|
|
|
std::cout << " (" << durationMs << " ms)";
|
2017-01-06 09:54:19 -06:00
|
|
|
}
|
|
|
|
|
2016-04-15 09:01:02 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|