Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 147544
b: refs/heads/master
c: a930d2c
h: refs/heads/master
v: v3
  • Loading branch information
Ingo Molnar committed May 27, 2009
1 parent e467ba9 commit 426f1fa
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 1 deletion.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 23ac9cbed82b00ca3520bb81dbe9ea3b7a936a1b
refs/heads/master: a930d2c0d0a685ab955472b08baad041cc5edb4a
4 changes: 4 additions & 0 deletions trunk/Documentation/perf_counter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,13 @@ LIB_H += util/util.h
LIB_H += util/help.h
LIB_H += util/strbuf.h
LIB_H += util/run-command.h
LIB_H += util/sigchain.h

LIB_OBJS += util/abspath.o
LIB_OBJS += util/alias.o
LIB_OBJS += util/config.o
LIB_OBJS += util/ctype.o
LIB_OBJS += util/environment.o
LIB_OBJS += util/exec_cmd.o
LIB_OBJS += util/help.o
LIB_OBJS += util/levenshtein.o
Expand All @@ -314,6 +316,8 @@ LIB_OBJS += util/quote.o
LIB_OBJS += util/strbuf.o
LIB_OBJS += util/usage.o
LIB_OBJS += util/wrapper.o
LIB_OBJS += util/sigchain.o
LIB_OBJS += util/pager.o

BUILTIN_OBJS += builtin-help.o
BUILTIN_OBJS += builtin-record.o
Expand Down
3 changes: 3 additions & 0 deletions trunk/Documentation/perf_counter/builtin-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ctype.h>

#include "util/list.h"
#include "util/cache.h"
#include "util/rbtree.h"

#include "perf.h"
Expand Down Expand Up @@ -992,5 +993,7 @@ int cmd_report(int argc, const char **argv, const char *prefix)

parse_options(argc, argv, options, report_usage, 0);

setup_pager();

return __cmd_report();
}
8 changes: 8 additions & 0 deletions trunk/Documentation/perf_counter/util/environment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* We put all the perf config variables in this same object
* file, so that programs can link against the config parser
* without having to link against all the rest of perf.
*/
#include "cache.h"

const char *pager_program;
99 changes: 99 additions & 0 deletions trunk/Documentation/perf_counter/util/pager.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "cache.h"
#include "run-command.h"
#include "sigchain.h"

/*
* This is split up from the rest of git so that we can do
* something different on Windows.
*/

static int spawned_pager;

#ifndef __MINGW32__
static void pager_preexec(void)
{
/*
* Work around bug in "less" by not starting it until we
* have real input
*/
fd_set in;

FD_ZERO(&in);
FD_SET(0, &in);
select(1, &in, NULL, &in, NULL);

setenv("LESS", "FRSX", 0);
}
#endif

static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
static struct child_process pager_process;

static void wait_for_pager(void)
{
fflush(stdout);
fflush(stderr);
/* signal EOF to pager */
close(1);
close(2);
finish_command(&pager_process);
}

static void wait_for_pager_signal(int signo)
{
wait_for_pager();
sigchain_pop(signo);
raise(signo);
}

void setup_pager(void)
{
const char *pager = getenv("PERF_PAGER");

if (!isatty(1))
return;
if (!pager) {
if (!pager_program)
perf_config(perf_default_config, NULL);
pager = pager_program;
}
if (!pager)
pager = getenv("PAGER");
if (!pager)
pager = "less";
else if (!*pager || !strcmp(pager, "cat"))
return;

spawned_pager = 1; /* means we are emitting to terminal */

/* spawn the pager */
pager_argv[2] = pager;
pager_process.argv = pager_argv;
pager_process.in = -1;
#ifndef __MINGW32__
pager_process.preexec_cb = pager_preexec;
#endif
if (start_command(&pager_process))
return;

/* original process continues, but writes to the pipe */
dup2(pager_process.in, 1);
if (isatty(2))
dup2(pager_process.in, 2);
close(pager_process.in);

/* this makes sure that the parent terminates after the pager */
sigchain_push_common(wait_for_pager_signal);
atexit(wait_for_pager);
}

int pager_in_use(void)
{
const char *env;

if (spawned_pager)
return 1;

env = getenv("PERF_PAGER_IN_USE");
return env ? perf_config_bool("PERF_PAGER_IN_USE", env) : 0;
}
52 changes: 52 additions & 0 deletions trunk/Documentation/perf_counter/util/sigchain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "sigchain.h"
#include "cache.h"

#define SIGCHAIN_MAX_SIGNALS 32

struct sigchain_signal {
sigchain_fun *old;
int n;
int alloc;
};
static struct sigchain_signal signals[SIGCHAIN_MAX_SIGNALS];

static void check_signum(int sig)
{
if (sig < 1 || sig >= SIGCHAIN_MAX_SIGNALS)
die("BUG: signal out of range: %d", sig);
}

int sigchain_push(int sig, sigchain_fun f)
{
struct sigchain_signal *s = signals + sig;
check_signum(sig);

ALLOC_GROW(s->old, s->n + 1, s->alloc);
s->old[s->n] = signal(sig, f);
if (s->old[s->n] == SIG_ERR)
return -1;
s->n++;
return 0;
}

int sigchain_pop(int sig)
{
struct sigchain_signal *s = signals + sig;
check_signum(sig);
if (s->n < 1)
return 0;

if (signal(sig, s->old[s->n - 1]) == SIG_ERR)
return -1;
s->n--;
return 0;
}

void sigchain_push_common(sigchain_fun f)
{
sigchain_push(SIGINT, f);
sigchain_push(SIGHUP, f);
sigchain_push(SIGTERM, f);
sigchain_push(SIGQUIT, f);
sigchain_push(SIGPIPE, f);
}
11 changes: 11 additions & 0 deletions trunk/Documentation/perf_counter/util/sigchain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef SIGCHAIN_H
#define SIGCHAIN_H

typedef void (*sigchain_fun)(int);

int sigchain_push(int sig, sigchain_fun f);
int sigchain_pop(int sig);

void sigchain_push_common(sigchain_fun f);

#endif /* SIGCHAIN_H */

0 comments on commit 426f1fa

Please sign in to comment.