PostgreSQL Source Code  git master
pg_test_timing.c File Reference
#include "postgres_fe.h"
#include <limits.h>
#include "getopt_long.h"
#include "portability/instr_time.h"
Include dependency graph for pg_test_timing.c:

Go to the source code of this file.

Functions

static void handle_args (int argc, char *argv[])
 
static uint64 test_timing (unsigned int duration)
 
static void output (uint64 loop_count)
 
int main (int argc, char *argv[])
 

Variables

static const char * progname
 
static unsigned int test_duration = 3
 
long long int histogram [32]
 

Function Documentation

◆ handle_args()

static void handle_args ( int  argc,
char *  argv[] 
)
static

Definition at line 43 of file pg_test_timing.c.

References _, fprintf, getopt_long(), ngettext, optarg, optind, printf, progname, required_argument, and test_duration.

Referenced by main().

44 {
45  static struct option long_options[] = {
46  {"duration", required_argument, NULL, 'd'},
47  {NULL, 0, NULL, 0}
48  };
49 
50  int option; /* Command line option */
51  int optindex = 0; /* used by getopt_long */
52  unsigned long optval; /* used for option parsing */
53  char *endptr;
54 
55  if (argc > 1)
56  {
57  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
58  {
59  printf(_("Usage: %s [-d DURATION]\n"), progname);
60  exit(0);
61  }
62  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
63  {
64  puts("pg_test_timing (PostgreSQL) " PG_VERSION);
65  exit(0);
66  }
67  }
68 
69  while ((option = getopt_long(argc, argv, "d:",
70  long_options, &optindex)) != -1)
71  {
72  switch (option)
73  {
74  case 'd':
75  errno = 0;
76  optval = strtoul(optarg, &endptr, 10);
77 
78  if (endptr == optarg || *endptr != '\0' ||
79  errno != 0 || optval != (unsigned int) optval)
80  {
81  fprintf(stderr, _("%s: invalid argument for option %s\n"),
82  progname, "--duration");
83  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
84  exit(1);
85  }
86 
87  test_duration = (unsigned int) optval;
88  if (test_duration == 0)
89  {
90  fprintf(stderr, _("%s: %s must be in range %u..%u\n"),
91  progname, "--duration", 1, UINT_MAX);
92  exit(1);
93  }
94  break;
95 
96  default:
97  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
98  progname);
99  exit(1);
100  break;
101  }
102  }
103 
104  if (argc > optind)
105  {
106  fprintf(stderr,
107  _("%s: too many command-line arguments (first is \"%s\")\n"),
108  progname, argv[optind]);
109  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
110  progname);
111  exit(1);
112  }
113 
114 
115  printf(ngettext("Testing timing overhead for %u second.\n",
116  "Testing timing overhead for %u seconds.\n",
117  test_duration),
118  test_duration);
119 }
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
#define printf(...)
Definition: port.h:221
#define fprintf
Definition: port.h:219
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
#define ngettext(s, p, n)
Definition: c.h:1124
static const char * progname
char * optarg
Definition: getopt.c:52
#define _(x)
Definition: elog.c:88
static unsigned int test_duration

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file pg_test_timing.c.

References get_progname(), handle_args(), output(), PG_TEXTDOMAIN, progname, set_pglocale_pgservice(), test_duration, and test_timing().

27 {
28  uint64 loop_count;
29 
30  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_test_timing"));
31  progname = get_progname(argv[0]);
32 
33  handle_args(argc, argv);
34 
35  loop_count = test_timing(test_duration);
36 
37  output(loop_count);
38 
39  return 0;
40 }
static uint64 test_timing(unsigned int duration)
const char * get_progname(const char *argv0)
Definition: path.c:453
static void output(uint64 loop_count)
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1157
static void handle_args(int argc, char *argv[])
static const char * progname
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
static unsigned int test_duration

◆ output()

static void output ( uint64  loop_count)
static

Definition at line 182 of file pg_test_timing.c.

References _, histogram, i, Max, and printf.

Referenced by _crypt_blowfish_rn(), _crypt_gensalt_blowfish_rn(), _crypt_gensalt_extended_rn(), _crypt_gensalt_md5_rn(), _crypt_gensalt_traditional_rn(), adjust_data_dir(), auth_peer(), be_gssapi_read(), be_gssapi_write(), brin_page_items(), CheckSCRAMAuth(), d2d(), dttofmtasc_replace(), exec_command_sf_sv(), f2d(), get_bin_version(), get_control_data(), helpSQL(), helpVariables(), main(), pg_GSS_read(), pg_GSS_write(), pg_SASL_continue(), pqsecure_open_gss(), printHistory(), px_crypt_des(), secure_open_gssapi(), slashUsage(), statext_dependencies_serialize(), statext_ndistinct_serialize(), tbm_iterate(), tbm_shared_iterate(), to_chars(), to_chars_df(), to_chars_f(), usage(), and worker_freeze_result_tape().

183 {
184  int64 max_bit = 31,
185  i;
186  char *header1 = _("< us");
187  char *header2 = /* xgettext:no-c-format */ _("% of total");
188  char *header3 = _("count");
189  int len1 = strlen(header1);
190  int len2 = strlen(header2);
191  int len3 = strlen(header3);
192 
193  /* find highest bit value */
194  while (max_bit > 0 && histogram[max_bit] == 0)
195  max_bit--;
196 
197  printf(_("Histogram of timing durations:\n"));
198  printf("%*s %*s %*s\n",
199  Max(6, len1), header1,
200  Max(10, len2), header2,
201  Max(10, len3), header3);
202 
203  for (i = 0; i <= max_bit; i++)
204  printf("%*ld %*.5f %*lld\n",
205  Max(6, len1), 1l << i,
206  Max(10, len2) - 1, (double) histogram[i] * 100 / loop_count,
207  Max(10, len3), histogram[i]);
208 }
#define printf(...)
Definition: port.h:221
long long int histogram[32]
#define Max(x, y)
Definition: c.h:922
int i
#define _(x)
Definition: elog.c:88

◆ test_timing()

static uint64 test_timing ( unsigned int  duration)
static

Definition at line 122 of file pg_test_timing.c.

References _, cur, end_time, fprintf, histogram, INSTR_TIME_GET_DOUBLE, INSTR_TIME_GET_MICROSEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, printf, and start_time.

Referenced by main().

123 {
124  uint64 total_time;
125  int64 time_elapsed = 0;
126  uint64 loop_count = 0;
127  uint64 prev,
128  cur;
130  end_time,
131  temp;
132 
133  total_time = duration > 0 ? duration * INT64CONST(1000000) : 0;
134 
135  INSTR_TIME_SET_CURRENT(start_time);
136  cur = INSTR_TIME_GET_MICROSEC(start_time);
137 
138  while (time_elapsed < total_time)
139  {
140  int32 diff,
141  bits = 0;
142 
143  prev = cur;
145  cur = INSTR_TIME_GET_MICROSEC(temp);
146  diff = cur - prev;
147 
148  /* Did time go backwards? */
149  if (diff < 0)
150  {
151  fprintf(stderr, _("Detected clock going backwards in time.\n"));
152  fprintf(stderr, _("Time warp: %d ms\n"), diff);
153  exit(1);
154  }
155 
156  /* What is the highest bit in the time diff? */
157  while (diff)
158  {
159  diff >>= 1;
160  bits++;
161  }
162 
163  /* Update appropriate duration bucket */
164  histogram[bits]++;
165 
166  loop_count++;
167  INSTR_TIME_SUBTRACT(temp, start_time);
168  time_elapsed = INSTR_TIME_GET_MICROSEC(temp);
169  }
170 
171  INSTR_TIME_SET_CURRENT(end_time);
172 
173  INSTR_TIME_SUBTRACT(end_time, start_time);
174 
175  printf(_("Per loop time including overhead: %0.2f ns\n"),
176  INSTR_TIME_GET_DOUBLE(end_time) * 1e9 / loop_count);
177 
178  return loop_count;
179 }
struct timeval instr_time
Definition: instr_time.h:150
struct cursor * cur
Definition: ecpg.c:28
#define printf(...)
Definition: port.h:221
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:199
#define fprintf
Definition: port.h:219
int duration
Definition: pgbench.c:147
static time_t start_time
Definition: pg_ctl.c:99
signed int int32
Definition: c.h:363
int64 end_time
Definition: pgbench.c:148
long long int histogram[32]
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:205
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
#define _(x)
Definition: elog.c:88

Variable Documentation

◆ histogram

long long int histogram[32]

Definition at line 23 of file pg_test_timing.c.

Referenced by output(), and test_timing().

◆ progname

const char* progname
static

Definition at line 14 of file pg_test_timing.c.

Referenced by handle_args(), and main().

◆ test_duration

unsigned int test_duration = 3
static

Definition at line 16 of file pg_test_timing.c.

Referenced by handle_args(), and main().