ladybird/Userland/Utilities/killall.cpp
Tom 7e77a2ec40 Everywhere: Improve CPU usage calculation
As threads come and go, we can't simply account for how many time
slices the threads at any given point may have been using. We need to
also account for threads that have since disappeared. This means we
also need to track how many time slices we have expired globally.

However, because this doesn't account for context switches outside of
the system timer tick values may still be under-reported. To solve this
we will need to track more accurate time information on each context
switch.

This also fixes top's cpu usage calculation which was still based on
the number of context switches.

Fixes #6473
2021-07-18 22:08:26 +02:00

70 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static void print_usage_and_exit()
{
warnln("usage: killall [-signal] process_name");
exit(1);
}
static int kill_all(const String& process_name, const unsigned signum)
{
auto all_processes = Core::ProcessStatisticsReader::get_all();
if (!all_processes.has_value())
return 1;
for (auto& process : all_processes.value().processes) {
if (process.name == process_name) {
int ret = kill(process.pid, signum);
if (ret < 0)
perror("kill");
}
}
return 0;
}
int main(int argc, char** argv)
{
unsigned signum = SIGTERM;
int name_argi = 1;
if (argc != 2 && argc != 3)
print_usage_and_exit();
if (argc == 3) {
name_argi = 2;
if (argv[1][0] != '-')
print_usage_and_exit();
Optional<unsigned> number;
if (isalpha(argv[1][1])) {
int value = getsignalbyname(&argv[1][1]);
if (value >= 0 && value < NSIG)
number = value;
}
if (!number.has_value())
number = String(&argv[1][1]).to_uint();
if (!number.has_value()) {
warnln("'{}' is not a valid signal name or number", &argv[1][1]);
return 2;
}
signum = number.value();
}
return kill_all(argv[name_argi], signum);
}