/* * Copyright (c) 2020 Cisco and/or its affiliates. * 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 #include #include #include #include #include #include #include perfmon_main_t perfmon_main; VLIB_PLUGIN_REGISTER () = { .version = VPP_BUILD_VER, .description = "Performance Monitor", }; VLIB_REGISTER_LOG_CLASS (if_default_log, static) = { .class_name = "perfmon", }; #define log_debug(fmt, ...) \ vlib_log_debug (if_default_log.class, fmt, __VA_ARGS__) #define log_warn(fmt, ...) \ vlib_log_warn (if_default_log.class, fmt, __VA_ARGS__) #define log_err(fmt, ...) vlib_log_err (if_default_log.class, fmt, __VA_ARGS__) void perfmon_reset (vlib_main_t *vm) { perfmon_main_t *pm = &perfmon_main; uword page_size = clib_mem_get_page_size (); if (pm->is_running) for (int i = 0; i < vlib_get_n_threads (); i++) vlib_node_set_dispatch_wrapper (vlib_get_main_by_index (i), 0); for (int i = 0; i < vec_len (pm->fds_to_close); i++) close (pm->fds_to_close[i]); vec_free (pm->fds_to_close); vec_free (pm->group_fds); if (pm->default_instance_type) { perfmon_instance_type_t *it = pm->default_instance_type; for (int i = 0; i < vec_len (it->instances); i++) vec_free (it->instances[i].name); vec_free (it->instances); vec_free (pm->default_instance_type); } for (int i = 0; i < vec_len (pm->thread_runtimes); i++) { perfmon_thread_runtime_t *tr = vec_elt_at_index (pm->thread_runtimes, i); vec_free (tr->node_stats); for (int j = 0; j < PERF_MAX_EVENTS; j++) if (tr->mmap_pages[j]) munmap (tr->mmap_pages[j], page_size); } vec_free (pm->thread_runtimes); pm->is_running = 0; pm->active_instance_type = 0; pm->active_bundle = 0; } static clib_error_t * perfmon_set (vlib_main_t *vm, perfmon_bundle_t *b) { clib_error_t *err = 0; perfmon_main_t *pm = &perfmon_main; perfmon_source_t *s; int is_node = 0; int n_nodes = vec_len (vm->node_main.nodes); uword page_size = clib_mem_get_page_size (); u32 instance_type = 0; perfmon_event_t *e; perfmon_instance_type_t *it = 0; perfmon_reset (vm); s = b->src; ASSERT (b->n_events); if (b->active_type == PERFMON_BUNDLE_TYPE_NODE) is_node = 1; if (s->instances_by_type == 0) { vec_add2 (pm->default_instance_type, it, 1); it->name = is_node ? "Thread/Node" : "Thread"; for (int i = 0; i < vlib_get_n_threads (); i++) { vlib_worker_thread_t *w = vlib_worker_threads + i; perfmon_instance_t *in; vec_add2 (it->instances, in, 1); in->cpu = w->cpu_id; in->pid = w->lwp; in->name = (char *) format (0, "%s (%u)%c", w->name, i, 0); } if (is_node) vec_validate (pm->thread_runtimes, vlib_get_n_threads () - 1); } else { e = s->events + b->events[0]; if (e->type_from_instance) { instance_type = e->instance_type; for (int i = 1; i < b->n_events; i++) { e = s->events + b->events[i]; ASSERT (e->type_from_instance == 1 && e->instance_type == instance_type); } } it = vec_elt_at_index (s->instances_by_type, instance_type); } pm->active_instance_type = it; for (int i = 0; i < vec_len (it->instances); i++) { perfmon_instance_t *in = vec_elt_at_index (it->instances, i); vec_validate (pm->group_fds, i); pm->group_fds[i] = -1; for (int j = 0; j < b->n_events; j++) { int fd; perfmon_event_t *e = s->events + b->events[j]; struct perf_event_attr pe = { .size = sizeof (struct perf_event_attr), .type = e->type_from_instance ? in->type : e->type, .config = e->config, .exclude_kernel = e->exclude_kernel, .read_format = (PERF_FORMAT_GROUP | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING), .disabled = 1, }; log_debug ("perf_event_open pe.type=%u pe.config=0x%x pid=%d " "cpu=%d group_fd=%d", pe.type, pe.config, in->pid, in->cpu, pm->group_fds[i]);