blob: e90a0d0c40f98ddeb25e1ac169819b09737ed67c [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <lib/zircon-internal/ktrace.h>
#include <array>
#include <iterator>
#include <map>
#include <string>
#include <vector>
#include <trace-reader/reader.h>
#include <trace-reader/records.h>
#include "utility.h"
class Tracing {
Tracing() { Stop(); }
Tracing(const Tracing&) = delete;
Tracing& operator=(const Tracing&) = delete;
Tracing(Tracing&&) = delete;
Tracing& operator=(Tracing&&) = delete;
virtual ~Tracing() { Stop(); }
struct DurationStats {
uint64_t begin_ts_ns = 0;
uint64_t end_ts_ns = 0;
uint64_t wall_duration_ns = 0;
std::vector<trace::Argument> arguments;
explicit DurationStats(uint64_t begin) : begin_ts_ns(begin) {}
DurationStats(uint64_t begin, uint64_t end, uint64_t wall_duration_ns,
std::vector<trace::Argument> arguments)
: begin_ts_ns(begin),
arguments(std::move(arguments)) {}
struct QueuingStats {
uint64_t begin_ts_ns = 0;
uint64_t end_ts_ns = 0;
uint64_t queuing_time_ns = 0;
uint64_t associated_thread = 0;
QueuingStats(uint64_t begin, uint64_t thread) {
begin_ts_ns = begin;
associated_thread = thread;
// Rewinds kernel trace buffer.
void Rewind();
// Starts kernel tracing.
void Start(uint32_t group_mask);
// Stops kernel tracing.
void Stop();
// Reads trace buffer and converts output into human-readable format. Stores in location defined
// by <filepath>. Will overwrite any existing files with same name.
bool WriteHumanReadable(std::ostream& filepath);
// Picks out traces pertaining to name in string_ref and runs stats on them. Returns false if name
// not found.
bool PopulateDurationStats(std::string string_ref, std::vector<DurationStats>* duration_stats,
std::map<uint64_t, QueuingStats>* queuing_stats);
bool running() const { return running_; }
enum EventState {
// Performs same action as zx_ktrace_read and does necessary checks.
virtual void ReadKernelBuffer(zx_handle_t handle, void* data_buf, uint32_t offset, size_t len,
size_t* bytes_read);
size_t ReadKernelRecords(trace::TraceReader::RecordConsumer consumer,
trace::TraceReader::ErrorHandler error_handler);
zx_handle_t debug_resource_ = GetDebugResource()->get();
bool running_ = false;