mirror of
https://github.com/fish-shell/fish-shell
synced 2024-11-10 23:24:39 +00:00
Add print-rusage-self to fish
This adds an option --print-rusage-self to the fish executable. When set, this option prints some getrusage stats to the console in a human-readable way. This will be used by upcoming benchmarking support.
This commit is contained in:
parent
25dd22242d
commit
b6555a0dc4
4 changed files with 49 additions and 0 deletions
|
@ -61,6 +61,7 @@ CHECK_CXX_SYMBOL_EXISTS(futimens sys/stat.h HAVE_FUTIMENS)
|
|||
CHECK_CXX_SYMBOL_EXISTS(futimes sys/time.h HAVE_FUTIMES)
|
||||
CHECK_CXX_SYMBOL_EXISTS(getifaddrs ifaddrs.h HAVE_GETIFADDRS)
|
||||
CHECK_CXX_SYMBOL_EXISTS(getpwent pwd.h HAVE_GETPWENT)
|
||||
CHECK_CXX_SYMBOL_EXISTS(getrusage sys/resource.h HAVE_GETRUSAGE)
|
||||
CHECK_CXX_SYMBOL_EXISTS(gettext libintl.h HAVE_GETTEXT)
|
||||
CHECK_CXX_SYMBOL_EXISTS(killpg "sys/types.h;signal.h" HAVE_KILLPG)
|
||||
CHECK_CXX_SYMBOL_EXISTS(lrand48_r stdlib.h HAVE_LRAND48_R)
|
||||
|
|
|
@ -31,6 +31,9 @@
|
|||
/* Define to 1 if you have the `getpwent' function. */
|
||||
#cmakedefine HAVE_GETPWENT 1
|
||||
|
||||
/* Define to 1 if you have the 'getrusage' function. */
|
||||
#cmakedefine HAVE_GETRUSAGE 1
|
||||
|
||||
/* Define to 1 if you have the `gettext' function. */
|
||||
#cmakedefine HAVE_GETTEXT 1
|
||||
|
||||
|
|
|
@ -30,6 +30,8 @@ The following options are available:
|
|||
|
||||
- ``-p`` or ``--profile=PROFILE_FILE`` when fish exits, output timing information on all executed commands to the specified file
|
||||
|
||||
- ``--print-rusage-self`` when fish exits, output stats from getrusage
|
||||
|
||||
- ``-v`` or ``--version`` display version and exit
|
||||
|
||||
- ``-D`` or ``--debug-stack-frames=DEBUG_LEVEL`` specify how many stack frames to display when debug messages are written. The default is zero. A value of 3 or 4 is usually sufficient to gain insight into how a given debug call was reached but you can specify a value up to 128.
|
||||
|
|
43
src/fish.cpp
43
src/fish.cpp
|
@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstring>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <cwchar>
|
||||
|
@ -63,11 +64,45 @@ class fish_cmd_opts_t {
|
|||
std::vector<std::string> batch_cmds;
|
||||
// Commands to execute after the shell's config has been read.
|
||||
std::vector<std::string> postconfig_cmds;
|
||||
/// Whether to print rusage-self stats after execution.
|
||||
bool print_rusage_self{false};
|
||||
};
|
||||
|
||||
/// If we are doing profiling, the filename to output to.
|
||||
static const char *s_profiling_output_filename = NULL;
|
||||
|
||||
/// \return a timeval converted to milliseconds.
|
||||
long long tv_to_msec(const struct timeval &tv) {
|
||||
long long msec = (long long)tv.tv_sec * 1000; // milliseconds per second
|
||||
msec += tv.tv_usec / 1000; // microseconds per millisecond
|
||||
return msec;
|
||||
}
|
||||
|
||||
static void print_rusage_self(FILE *fp) {
|
||||
#ifndef HAVE_GETRUSAGE
|
||||
fprintf(fp, "getrusage() not supported on this platform");
|
||||
return;
|
||||
#else
|
||||
struct rusage rs;
|
||||
if (getrusage(RUSAGE_SELF, &rs)) {
|
||||
perror("getrusage");
|
||||
return;
|
||||
}
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
// Macs use bytes.
|
||||
long rss_kb = rs.ru_maxrss / 1024;
|
||||
#else
|
||||
// Everyone else uses KB.
|
||||
long rss_kb = rs.ru_maxrss;
|
||||
#endif
|
||||
fprintf(fp, " rusage self:\n");
|
||||
fprintf(fp, " user time: %llu ms\n", tv_to_msec(rs.ru_utime));
|
||||
fprintf(fp, " sys time: %llu ms\n", tv_to_msec(rs.ru_stime));
|
||||
fprintf(fp, " max rss: %ld kb\n", rss_kb);
|
||||
fprintf(fp, " signals: %ld\n", rs.ru_nsignals);
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool has_suffix(const std::string &path, const char *suffix, bool ignore_case) {
|
||||
size_t pathlen = path.size(), suffixlen = std::strlen(suffix);
|
||||
return pathlen >= suffixlen &&
|
||||
|
@ -223,6 +258,7 @@ static int fish_parse_opt(int argc, char **argv, fish_cmd_opts_t *opts) {
|
|||
{"interactive", no_argument, NULL, 'i'},
|
||||
{"login", no_argument, NULL, 'l'},
|
||||
{"no-execute", no_argument, NULL, 'n'},
|
||||
{"print-rusage-self", no_argument, NULL, 1},
|
||||
{"profile", required_argument, NULL, 'p'},
|
||||
{"private", no_argument, NULL, 'P'},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
|
@ -275,6 +311,10 @@ static int fish_parse_opt(int argc, char **argv, fish_cmd_opts_t *opts) {
|
|||
no_exec = 1;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
opts->print_rusage_self = true;
|
||||
break;
|
||||
}
|
||||
case 'p': {
|
||||
s_profiling_output_filename = optarg;
|
||||
g_profiling_active = true;
|
||||
|
@ -450,6 +490,9 @@ int main(int argc, char **argv) {
|
|||
|
||||
history_save_all();
|
||||
proc_destroy();
|
||||
if (opts.print_rusage_self) {
|
||||
print_rusage_self(stderr);
|
||||
}
|
||||
exit_without_destructors(exit_status);
|
||||
return EXIT_FAILURE; // above line should always exit
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue