Skip to content

Commit

Permalink
spawn/Client: export statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jul 25, 2024
1 parent 28b3f99 commit 9e2576d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/spawn/Client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,11 @@ Serialize(SpawnSerializer &s, const PreparedChildProcess &p)
std::unique_ptr<ChildProcessHandle>
SpawnServerClient::SpawnChildProcess(const char *name,
PreparedChildProcess &&p)
{
try {
assert(!shutting_down);

++stats.spawned;

/* this check is performed again on the server (which is obviously
necessary, and the only way to have it secure); this one is
only here for the developer to see the error earlier in the
Expand Down Expand Up @@ -467,6 +469,9 @@ SpawnServerClient::SpawnChildProcess(const char *name,
auto handle = std::make_unique<ChildProcess>(*this, pid);
processes.insert(*handle);
return handle;
} catch (...) {
++stats.errors;
throw;
}

void
Expand Down Expand Up @@ -526,10 +531,12 @@ SpawnServerClient::HandleExecCompleteMessage(SpawnPayload payload)
if (const auto i = processes.find(pid); i != processes.end()) {
// TODO forward errors
if (i->completion_handler) {
if (*error == 0)
if (*error == 0) {
i->completion_handler->OnSpawnSuccess();
else
} else {
++stats.errors;
i->completion_handler->OnSpawnError(std::make_exception_ptr(std::runtime_error{error}));
}

/* if there is a completion handler,
don't log error message to
Expand Down
12 changes: 11 additions & 1 deletion src/spawn/Client.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Interface.hxx"
#include "Config.hxx"
#include "Stats.hxx"
#include "event/DeferEvent.hxx"
#include "event/SocketEvent.hxx"
#include "net/MultiReceiveMessage.hxx"
Expand Down Expand Up @@ -47,7 +48,9 @@ class SpawnServerClient final : public SpawnService {
IntrusiveHashSetOperators<ChildProcess,
ChildProcessGetKey,
std::hash<unsigned>,
std::equal_to<unsigned>>>;
std::equal_to<unsigned>>,
IntrusiveHashSetBaseHookTraits<ChildProcess>,
IntrusiveHashSetOptions{.constant_time_size=true}>;

ChildProcessSet processes;

Expand All @@ -63,6 +66,8 @@ class SpawnServerClient final : public SpawnService {

MultiReceiveMessage receive{16, 1024, CMSG_SPACE(sizeof(int)), 1};

mutable SpawnStats stats{};

unsigned last_pid = 0;

/**
Expand Down Expand Up @@ -107,6 +112,11 @@ public:
return cgroups;
}

const SpawnStats &GetStats() const noexcept {
stats.alive = processes.size();
return stats;
}

void Shutdown() noexcept;

UniqueSocketDescriptor Connect();
Expand Down
25 changes: 25 additions & 0 deletions src/spawn/Stats.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

#pragma once

#include <cstdint>

struct SpawnStats {
/**
* The total number of SpawnChildProcess() calls (include
* failed ones).
*/
uint_least64_t spawned;

/**
* The number of failed SpawnChildProcess() calls.
*/
uint_least64_t errors;

/**
* How many child processse are alive right now?
*/
std::size_t alive;
};

0 comments on commit 9e2576d

Please sign in to comment.