summaryrefslogtreecommitdiffstats
path: root/test/debug_internal.py
AgeCommit message (Collapse)AuthorFilesLines
2018-02-17make test: add test framework debuggingKlement Sekera1-0/+39
New option TEST_DEBUG=1 turns on test framework debugging, which currently consists of printing difference in allocated objects/memory and also creates reference graphs for any unfreed VppPapiProvider/VPP objects - these take a lot of memory and thus should be freed regularly. Change-Id: I29db0c1341009d4b5c5df9222d14f3095883fd0f Signed-off-by: Klement Sekera <ksekera@cisco.com>
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/*
 * perfmon_periodic.c - skeleton plug-in periodic function
 *
 * Copyright (c) <current-year> <your-organization>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vlib/vlib.h>
#include <vppinfra/error.h>
#include <perfmon/perfmon.h>
#include <asm/unistd.h>
#include <sys/ioctl.h>

/* "not in glibc" */
static long
perf_event_open (struct perf_event_attr *hw_event, pid_t pid, int cpu,
		 int group_fd, unsigned long flags)
{
  int ret;

  ret = syscall (__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
  return ret;
}

static void
read_current_perf_counters (vlib_main_t * vm, u64 * c0, u64 * c1,
			    vlib_node_runtime_t * node,
			    vlib_frame_t * frame, int before_or_after)
{
  int i;
  u64 *cc;
  perfmon_main_t *pm = &perfmon_main;
  uword my_thread_index = vm->thread_index;

  *c0 = *c1 = 0;

  for (i = 0; i < pm->n_active; i++)
    {
      cc = (i == 0) ? c0 : c1;
      if (pm->rdpmc_indices[i][my_thread_index] != ~0)
	*cc = clib_rdpmc ((int) pm->rdpmc_indices[i][my_thread_index]);
      else
	{
	  u64 sw_value;
	  int read_result;
	  if ((read_result = read (pm->pm_fds[i][my_thread_index], &sw_value,
				   sizeof (sw_value)) != sizeof (sw_value)))
	    {
	      clib_unix_warning
		("counter read returned %d, expected %d",
		 read_result, sizeof (sw_value));
	      clib_callback_enable_disable
		(vm->vlib_node_runtime_perf_counter_cbs,
		 vm->vlib_node_runtime_perf_counter_cb_tmp,
		 vm->worker_thread_main_loop_callback_lock,
		 read_current_perf_counters, 0 /* enable */ );
	      return;
	    }
	  *cc = sw_value;
	}
    }
}

static void
clear_counters (perfmon_main_t * pm)
{
  int i, j;
  vlib_main_t *vm = pm->vlib_main;
  vlib_main_t *stat_vm;
  vlib_node_main_t *nm;
  vlib_node_t *n;

  vlib_worker_thread_barrier_sync (vm);

  for (j = 0; j < vec_len (vlib_mains); j++)
    {
      stat_vm = vlib_mains[j];
      if (stat_vm == 0)
	continue;

      nm = &stat_vm->node_main;

      /* Clear the node runtime perfmon counters */
      for (i = 0; i < vec_len (nm->nodes); i++)
	{
	  n = nm->nodes[i];
	  vlib_node_sync_stats (stat_vm, n);
	}

      /* And clear the node perfmon counters */
      for (i = 0; i < vec_len (nm->nodes); i++)
	{
	  n = nm->nodes[i];
	  n->stats_total.perf_counter0_ticks = 0;
	  n->stats_total.perf_counter1_ticks = 0;
	  n->stats_total.perf_counter_vectors = 0;
	  n->stats_last_clear.perf_counter0_ticks = 0;
	  n->stats_last_clear.perf_counter1_ticks = 0;
	  n->stats_last_clear.perf_counter_vectors = 0;
	}
    }
  vlib_worker_thread_barrier_release (vm);
}

static void
enable_current_events (perfmon_main_t * pm)
{
  struct perf_event_attr pe;
  int fd;
  struct perf_event_mmap_page *p = 0;
  perfmon_event_config_t *c;
  vlib_main_t *vm = vlib_get_main ();
  u32 my_thread_index = vm->thread_index;
  u32 index;
  int i, limit = 1;
  int cpu;

  if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
    limit = 2;

  for (i = 0; i < limit; i++)
    {
      vec_validate (pm->pm_fds[i], vec_len (vlib_mains) - 1);
      vec_validate (pm->perf_event_pages[i], vec_len (vlib_mains) - 1);
      vec_validate (pm->rdpmc_indices[i], vec_len (vlib_mains) - 1);

      c = vec_elt_at_index (pm->single_events_to_collect,
			    pm->current_event + i);

      memset (&pe, 0, sizeof (struct perf_event_attr));
      pe.type = c->pe_type;
      pe.size = sizeof (struct perf_event_attr);
      pe.config = c->pe_config;
      pe.disabled = 1;
      pe.pinned = 1;
      /*
       * Note: excluding the kernel makes the
       * (software) context-switch counter read 0...
       */
      if (pe.type != PERF_TYPE_SOFTWARE)
	{
	  /* Exclude kernel and hypervisor */
	  pe.exclude_kernel = 1;
	  pe.exclude_hv = 1;
	}

      cpu = vm->cpu_id;

      fd = perf_event_open (&pe, 0, cpu, -1, 0);
      if (fd == -1)
	{
	  clib_unix_warning ("event open: type %d config %d", c->pe_type,
			     c->pe_config);
	  return;
	}

      if (pe.type != PERF_TYPE_SOFTWARE)
	{
	  p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
	  if (p == MAP_FAILED)
	    {
	      clib_unix_warning ("mmap");
	      close (fd);
	      return;
	    }
	  CLIB_MEM_UNPOISON (p, pm->page_size);
	}
      else
	p = 0;

      if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
	clib_unix_warning ("reset ioctl");

      if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
	clib_unix_warning ("enable ioctl");

      pm->perf_event_pages[i][my_thread_index] = (void *) p;
      pm->pm_fds[i][my_thread_index] = fd;
    }

  /*
   * Hardware events must be all opened and enabled before aquiring
   * pmc indices, otherwise the pmc indices might be out-dated.
   */
  for (i = 0; i < limit; i++)
    {
      p =
	(struct perf_event_mmap_page *)
	pm->perf_event_pages[i][my_thread_index];

      /*
       * Software event counters - and others not capable of being
       * read via the "rdpmc" instruction - will be read
       * by system calls.
       */
      if (p == 0 || p->cap_user_rdpmc == 0)
	index = ~0;
      else
	index = p->index - 1;

      pm->rdpmc_indices[i][my_thread_index] = index;
    }

  pm->n_active = i;
  /* Enable the main loop counter snapshot mechanism */
  clib_callback_enable_disable
    (vm->vlib_node_runtime_perf_counter_cbs,
     vm->vlib_node_runtime_perf_counter_cb_tmp,
     vm->worker_thread_main_loop_callback_lock,
     read_current_perf_counters, 1 /* enable */ );
}

static void
disable_events (perfmon_main_t * pm)
{
  vlib_main_t *vm = vlib_get_main ();
  u32 my_thread_index = vm->thread_index;
  int i;

  /* Stop main loop collection */
  clib_callback_enable_disable
    (vm->vlib_node_runtime_perf_counter_cbs,
     vm->vlib_node_runtime_perf_counter_cb_tmp,
     vm->worker_thread_main_loop_callback_lock,
     read_current_perf_counters, 0 /* enable */ );

  for (i = 0; i < pm->n_active; i++)
    {
      if (pm->pm_fds[i][my_thread_index] == 0)
	continue;

      if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
	  0)
	clib_unix_warning ("disable ioctl");

      if (pm->perf_event_pages[i][my_thread_index])
	{
	  if (munmap (pm->perf_event_pages[i][my_thread_index],
		      pm->page_size) < 0)
	    clib_unix_warning ("munmap");
	  CLIB_MEM_POISON (pm->perf_event_pages[i][my_thread_index],
			   pm->page_size);
	  pm->perf_event_pages[i][my_thread_index] = 0;
	}

      (void) close (pm->pm_fds[i][my_thread_index]);
      pm->pm_fds[i][my_thread_index] = 0;

    }
}

static void
worker_thread_start_event (vlib_main_t * vm)
{
  perfmon_main_t *pm = &perfmon_main;

  clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
				vm->worker_thread_main_loop_callback_tmp,
				vm->worker_thread_main_loop_callback_lock,
				worker_thread_start_event, 0 /* enable */ );
  enable_current_events (pm);
}

static void
worker_thread_stop_event (vlib_main_t * vm)
{
  perfmon_main_t *pm = &perfmon_main;
  clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
				vm->worker_thread_main_loop_callback_tmp,
				vm->worker_thread_main_loop_callback_lock,
				worker_thread_stop_event, 0 /* enable */ );
  disable_events (pm);
}

static void
start_event (perfmon_main_t * pm, f64 now, uword event_data)
{
  int i;
  int last_set;
  int all = 0;
  pm->current_event = 0;

  if (vec_len (pm->single_events_to_collect) == 0)
    {
      pm->state = PERFMON_STATE_OFF;
      return;
    }

  last_set = clib_bitmap_last_set (pm->thread_bitmap);
  all = (last_set == ~0);

  pm->state = PERFMON_STATE_RUNNING;
  clear_counters (pm);

  /* Start collection on thread 0? */
  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
    {
      /* Start collection on this thread */
      enable_current_events (pm);
    }

  /* And also on worker threads */
  for (i = 1; i < vec_len (vlib_mains); i++)
    {
      if (vlib_mains[i] == 0)
	continue;

      if (all || clib_bitmap_get (pm->thread_bitmap, i))
	clib_callback_enable_disable
	  (vlib_mains[i]->worker_thread_main_loop_callbacks,
	   vlib_mains[i]->worker_thread_main_loop_callback_tmp,
	   vlib_mains[i]->worker_thread_main_loop_callback_lock,
	   (void *) worker_thread_start_event, 1 /* enable */ );
    }
}

void
scrape_and_clear_counters (perfmon_main_t * pm)
{
  int i, j, k;
  vlib_main_t *vm = pm->vlib_main;
  vlib_main_t *stat_vm;
  vlib_node_main_t *nm;
  vlib_node_t ***node_dups = 0;
  vlib_node_t **nodes;
  vlib_node_t *n;
  perfmon_capture_t *c;
  perfmon_event_config_t *current_event;
  uword *p;
  u8 *counter_name;
  u64 vectors_this_counter;

  /* snapshoot the nodes, including pm counters */
  vlib_worker_thread_barrier_sync (vm);

  for (j = 0; j < vec_len (vlib_mains); j++)
    {
      stat_vm = vlib_mains[j];
      if (stat_vm == 0)
	continue;

      nm = &stat_vm->node_main;

      for (i = 0; i < vec_len (nm->nodes); i++)
	{
	  n = nm->nodes[i];
	  vlib_node_sync_stats (stat_vm, n);
	}

      nodes = 0;
      vec_validate (nodes, vec_len (nm->nodes) - 1);
      vec_add1 (node_dups, nodes);

      /* Snapshoot and clear the per-node perfmon counters */
      for (i = 0; i < vec_len (nm->nodes); i++)
	{
	  n = nm->nodes[i];
	  nodes[i] = clib_mem_alloc (sizeof (*n));
	  clib_memcpy_fast (nodes[i], n, sizeof (*n));
	  n->stats_total.perf_counter0_ticks = 0;
	  n->stats_total.perf_counter1_ticks = 0;
	  n->stats_total.perf_counter_vectors = 0;
	  n->stats_last_clear.perf_counter0_ticks = 0;
	  n->stats_last_clear.perf_counter1_ticks = 0;
	  n->stats_last_clear.perf_counter_vectors = 0;
	}
    }

  vlib_worker_thread_barrier_release (vm);

  for (j = 0; j < vec_len (vlib_mains); j++)
    {
      stat_vm = vlib_mains[j];
      if (stat_vm == 0)
	continue;

      nodes = node_dups[j];

      for (i = 0; i < vec_len (nodes); i++)
	{
	  u8 *capture_name;

	  n = nodes[i];

	  if (n->stats_total.perf_counter0_ticks == 0 &&
	      n->stats_total.perf_counter1_ticks == 0)
	    goto skip_this_node;

	  for (k = 0; k < 2; k++)
	    {
	      u64 counter_value, counter_last_clear;

	      /*
	       * We collect 2 counters at once, except for the
	       * last counter when the user asks for an odd number of
	       * counters
	       */
	      if ((pm->current_event + k)
		  >= vec_len (pm->single_events_to_collect))
		break;

	      if (k == 0)
		{
		  counter_value = n->stats_total.perf_counter0_ticks;
		  counter_last_clear =
		    n->stats_last_clear.perf_counter0_ticks;
		}
	      else
		{
		  counter_value = n->stats_total.perf_counter1_ticks;
		  counter_last_clear =
		    n->stats_last_clear.perf_counter1_ticks;
		}

	      capture_name = format (0, "t%d-%v%c", j, n->name, 0);

	      p = hash_get_mem (pm->capture_by_thread_and_node_name,
				capture_name);

	      if (p == 0)
		{
		  pool_get (pm->capture_pool, c);
		  memset (c, 0, sizeof (*c));
		  c->thread_and_node_name = capture_name;
		  hash_set_mem (pm->capture_by_thread_and_node_name,
				capture_name, c - pm->capture_pool);
		}
	      else
		{
		  c = pool_elt_at_index (pm->capture_pool, p[0]);
		  vec_free (capture_name);
		}

	      /* Snapshoot counters, etc. into the capture */
	      current_event = pm->single_events_to_collect
		+ pm->current_event + k;
	      counter_name = (u8 *) current_event->name;
	      vectors_this_counter = n->stats_total.perf_counter_vectors -
		n->stats_last_clear.perf_counter_vectors;

	      vec_add1 (c->counter_names, counter_name);
	      vec_add1 (c->counter_values,
			counter_value - counter_last_clear);
	      vec_add1 (c->vectors_this_counter, vectors_this_counter);
	    }
	skip_this_node:
	  clib_mem_free (n);
	}
      vec_free (nodes);
    }
  vec_free (node_dups);
}

static void
handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
{
  int i;
  int last_set, all;

  last_set = clib_bitmap_last_set (pm->thread_bitmap);
  all = (last_set == ~0);

  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
    disable_events (pm);

  /* And also on worker threads */
  for (i = 1; i < vec_len (vlib_mains); i++)
    {
      if (vlib_mains[i] == 0)
	continue;
      if (all || clib_bitmap_get (pm->thread_bitmap, i))
	clib_callback_enable_disable
	  (vlib_mains[i]->worker_thread_main_loop_callbacks,
	   vlib_mains[i]->worker_thread_main_loop_callback_tmp,
	   vlib_mains[i]->worker_thread_main_loop_callback_lock,
	   (void *) worker_thread_stop_event, 1 /* enable */ );
    }

  /* Make sure workers have stopped collection */
  if (i > 1)
    {
      f64 deadman = vlib_time_now (vm) + 1.0;

      for (i = 1; i < vec_len (vlib_mains); i++)
	{
	  /* Has the worker actually stopped collecting data? */
	  while (clib_callback_is_set
		 (vlib_mains[i]->worker_thread_main_loop_callbacks,
		  vlib_mains[i]->worker_thread_main_loop_callback_lock,
		  read_current_perf_counters))
	    {
	      if (vlib_time_now (vm) > deadman)
		{
		  clib_warning ("Thread %d deadman timeout!", i);
		  break;
		}
	      vlib_process_suspend (pm->vlib_main, 1e-3);
	    }
	}
    }
  scrape_and_clear_counters (pm);
  pm->current_event += pm->n_active;
  if (pm->current_event >= vec_len (pm->single_events_to_collect))
    {
      pm->current_event = 0;
      pm->state = PERFMON_STATE_OFF;
      return;
    }

  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
    enable_current_events (pm);

  /* And also on worker threads */
  for (i = 1; i < vec_len (vlib_mains); i++)
    {
      if (vlib_mains[i] == 0)
	continue;
      if (all || clib_bitmap_get (pm->thread_bitmap, i))
	clib_callback_enable_disable
	  (vlib_mains[i]->worker_thread_main_loop_callbacks,
	   vlib_mains[i]->worker_thread_main_loop_callback_tmp,
	   vlib_mains[i]->worker_thread_main_loop_callback_lock,
	   worker_thread_start_event, 1 /* enable */ );
    }
}

static uword
perfmon_periodic_process (vlib_main_t * vm,
			  vlib_node_runtime_t * rt, vlib_frame_t * f)
{
  perfmon_main_t *pm = &perfmon_main;
  f64 now;
  uword *event_data = 0;
  uword event_type;
  int i;

  while (1)
    {
      if (pm->state == PERFMON_STATE_RUNNING)
	vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
      else
	vlib_process_wait_for_event (vm);

      now = vlib_time_now (vm);

      event_type = vlib_process_get_events (vm, (uword **) & event_data);

      switch (event_type)
	{
	case PERFMON_START:
	  for (i = 0; i < vec_len (event_data); i++)
	    start_event (pm, now, event_data[i]);
	  break;

	  /* Handle timeout */
	case ~0:
	  handle_timeout (vm, pm, now);
	  break;

	default:
	  clib_warning ("Unexpected event %d", event_type);
	  break;
	}
      vec_reset_length (event_data);
    }
  return 0;			/* or not */
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (perfmon_periodic_node) =
{
  .function = perfmon_periodic_process,
  .type = VLIB_NODE_TYPE_PROCESS,
  .name = "perfmon-periodic-process",
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */