Blame libipt/internal/include/pt_time.h

Packit b1f7ae
/*
Packit b1f7ae
 * Copyright (c) 2014-2017, Intel Corporation
Packit b1f7ae
 *
Packit b1f7ae
 * Redistribution and use in source and binary forms, with or without
Packit b1f7ae
 * modification, are permitted provided that the following conditions are met:
Packit b1f7ae
 *
Packit b1f7ae
 *  * Redistributions of source code must retain the above copyright notice,
Packit b1f7ae
 *    this list of conditions and the following disclaimer.
Packit b1f7ae
 *  * Redistributions in binary form must reproduce the above copyright notice,
Packit b1f7ae
 *    this list of conditions and the following disclaimer in the documentation
Packit b1f7ae
 *    and/or other materials provided with the distribution.
Packit b1f7ae
 *  * Neither the name of Intel Corporation nor the names of its contributors
Packit b1f7ae
 *    may be used to endorse or promote products derived from this software
Packit b1f7ae
 *    without specific prior written permission.
Packit b1f7ae
 *
Packit b1f7ae
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit b1f7ae
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit b1f7ae
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit b1f7ae
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit b1f7ae
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit b1f7ae
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit b1f7ae
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit b1f7ae
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit b1f7ae
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit b1f7ae
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit b1f7ae
 * POSSIBILITY OF SUCH DAMAGE.
Packit b1f7ae
 */
Packit b1f7ae
Packit b1f7ae
#ifndef PT_TIME_H
Packit b1f7ae
#define PT_TIME_H
Packit b1f7ae
Packit b1f7ae
#include <stdint.h>
Packit b1f7ae
Packit b1f7ae
struct pt_config;
Packit b1f7ae
struct pt_packet_tsc;
Packit b1f7ae
struct pt_packet_cbr;
Packit b1f7ae
struct pt_packet_tma;
Packit b1f7ae
struct pt_packet_mtc;
Packit b1f7ae
struct pt_packet_cyc;
Packit b1f7ae
Packit b1f7ae
Packit b1f7ae
/* Intel(R) Processor Trace timing. */
Packit b1f7ae
struct pt_time {
Packit b1f7ae
	/* The estimated Time Stamp Count. */
Packit b1f7ae
	uint64_t tsc;
Packit b1f7ae
Packit b1f7ae
	/* The base Time Stamp Count (from TSC and MTC). */
Packit b1f7ae
	uint64_t base;
Packit b1f7ae
Packit b1f7ae
	/* The estimated Fast Counter. */
Packit b1f7ae
	uint64_t fc;
Packit b1f7ae
Packit b1f7ae
	/* The adjusted last CTC value (from MTC and TMA). */
Packit b1f7ae
	uint32_t ctc;
Packit b1f7ae
Packit b1f7ae
	/* The adjusted CTC value when @fc was cleared (from MTC and TMA). */
Packit b1f7ae
	uint32_t ctc_cyc;
Packit b1f7ae
Packit b1f7ae
	/* The number of lost MTC updates. */
Packit b1f7ae
	uint32_t lost_mtc;
Packit b1f7ae
Packit b1f7ae
	/* The number of lost CYC updates. */
Packit b1f7ae
	uint32_t lost_cyc;
Packit b1f7ae
Packit b1f7ae
	/* The core:bus ratio. */
Packit b1f7ae
	uint8_t cbr;
Packit b1f7ae
Packit b1f7ae
	/* A flag saying whether we have seen a TSC packet. */
Packit b1f7ae
	uint32_t have_tsc:1;
Packit b1f7ae
Packit b1f7ae
	/* A flag saying whether we have seen a CBR packet. */
Packit b1f7ae
	uint32_t have_cbr:1;
Packit b1f7ae
Packit b1f7ae
	/* A flag saying whether we have seen a TMA packet. */
Packit b1f7ae
	uint32_t have_tma:1;
Packit b1f7ae
Packit b1f7ae
	/* A flag saying whether we have seen a MTC packet. */
Packit b1f7ae
	uint32_t have_mtc:1;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
/* Initialize (or reset) the time. */
Packit b1f7ae
extern void pt_time_init(struct pt_time *time);
Packit b1f7ae
Packit b1f7ae
/* Query the current time.
Packit b1f7ae
 *
Packit b1f7ae
 * Provides the estimated Time Stamp Count value in @tsc.
Packit b1f7ae
 *
Packit b1f7ae
 * If @lost_mtc is not NULL, provides the number of lost MTC packets.
Packit b1f7ae
 * If @lost_cyc is not NULL, provides the number of lost CYC packets.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success; a negative error code, otherwise.
Packit b1f7ae
 * Returns -pte_internal if @tsc or @time is NULL.
Packit b1f7ae
 * Returns -pte_no_time if there has not been a TSC packet.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_time_query_tsc(uint64_t *tsc, uint32_t *lost_mtc,
Packit b1f7ae
			     uint32_t *lost_cyc, const struct pt_time *time);
Packit b1f7ae
Packit b1f7ae
/* Query the current core:bus ratio.
Packit b1f7ae
 *
Packit b1f7ae
 * Provides the core:bus ratio in @cbr.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success; a negative error code, otherwise.
Packit b1f7ae
 * Returns -pte_internal if @cbr or @time is NULL.
Packit b1f7ae
 * Returns -pte_no_cbr if there has not been a CBR packet.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_time_query_cbr(uint32_t *cbr, const struct pt_time *time);
Packit b1f7ae
Packit b1f7ae
/* Update the time based on an Intel PT packet.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success.
Packit b1f7ae
 * Returns a negative error code, otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_time_update_tsc(struct pt_time *, const struct pt_packet_tsc *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_time_update_cbr(struct pt_time *, const struct pt_packet_cbr *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_time_update_tma(struct pt_time *, const struct pt_packet_tma *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_time_update_mtc(struct pt_time *, const struct pt_packet_mtc *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
/* @fcr is the fast-counter:cycles ratio obtained by calibration. */
Packit b1f7ae
extern int pt_time_update_cyc(struct pt_time *, const struct pt_packet_cyc *,
Packit b1f7ae
			      const struct pt_config *, uint64_t fcr);
Packit b1f7ae
Packit b1f7ae
Packit b1f7ae
/* Timing calibration.
Packit b1f7ae
 *
Packit b1f7ae
 * Used for estimating the Fast-Counter:Cycles ratio.
Packit b1f7ae
 *
Packit b1f7ae
 * Ideally, we calibrate by counting CYCs between MTCs.  Lacking MTCs, we
Packit b1f7ae
 * use TSC, instead.
Packit b1f7ae
 */
Packit b1f7ae
struct pt_time_cal {
Packit b1f7ae
	/* The estimated fast-counter:cycles ratio. */
Packit b1f7ae
	uint64_t fcr;
Packit b1f7ae
Packit b1f7ae
	/* The minimal and maximal @fcr values. */
Packit b1f7ae
	uint64_t min_fcr, max_fcr;
Packit b1f7ae
Packit b1f7ae
	/* The last TSC value.
Packit b1f7ae
	 *
Packit b1f7ae
	 * Used for calibrating at TSC.
Packit b1f7ae
	 */
Packit b1f7ae
	uint64_t tsc;
Packit b1f7ae
Packit b1f7ae
	/* The number of cycles since the last TSC (from CYC).
Packit b1f7ae
	 *
Packit b1f7ae
	 * Used for calibrating at TSC.
Packit b1f7ae
	 */
Packit b1f7ae
	uint64_t cyc_tsc;
Packit b1f7ae
Packit b1f7ae
	/* The number of cycles since the last MTC (from CYC).
Packit b1f7ae
	 *
Packit b1f7ae
	 * Used for calibrating at MTC.
Packit b1f7ae
	 */
Packit b1f7ae
	uint64_t cyc_mtc;
Packit b1f7ae
Packit b1f7ae
	/* The adjusted last CTC value (from MTC).
Packit b1f7ae
	 *
Packit b1f7ae
	 * Used for calibrating at MTC.
Packit b1f7ae
	 */
Packit b1f7ae
	uint32_t ctc;
Packit b1f7ae
Packit b1f7ae
	/* The number of lost MTC updates since the last successful update. */
Packit b1f7ae
	uint32_t lost_mtc;
Packit b1f7ae
Packit b1f7ae
	/* A flag saying whether we have seen a MTC packet. */
Packit b1f7ae
	uint32_t have_mtc:1;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
enum {
Packit b1f7ae
	/* The amount by which the fcr value is right-shifted.
Packit b1f7ae
	 *
Packit b1f7ae
	 * Do not shift the value obtained by pt_tcal_fcr() when passing it to
Packit b1f7ae
	 * pt_time_update_cyc().
Packit b1f7ae
	 * Do shift the value passed to pt_tcal_set_fcr().
Packit b1f7ae
	 */
Packit b1f7ae
	pt_tcal_fcr_shr	= 8
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
/* Initialize of reset timing calibration. */
Packit b1f7ae
extern void pt_tcal_init(struct pt_time_cal *tcal);
Packit b1f7ae
Packit b1f7ae
/* Query the estimated fast-counter:cycles ratio.
Packit b1f7ae
 *
Packit b1f7ae
 * Provides the estimated ratio in @fcr unless -pte_internal or
Packit b1f7ae
 * -pte_no_time is returned.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success, a negative error code otherwise.
Packit b1f7ae
 * Returns -pte_internal if @fcr or @tcal is NULL.
Packit b1f7ae
 * Returns -pte_no_time if no information is available.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_tcal_fcr(uint64_t *fcr, const struct pt_time_cal *tcal);
Packit b1f7ae
Packit b1f7ae
/* Set the fast-counter:cycles ratio.
Packit b1f7ae
 *
Packit b1f7ae
 * Timing calibration takes one CBR or two MTC packets before it can provide
Packit b1f7ae
 * first estimations.  Use this to supply an initial value to be used in the
Packit b1f7ae
 * meantime.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success, a negative error code otherwise.
Packit b1f7ae
 * Returns -pte_internal if @cal is NULL.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_tcal_set_fcr(struct pt_time_cal *tcal, uint64_t fcr);
Packit b1f7ae
Packit b1f7ae
/* Update calibration based on an Intel PT packet.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success, a negative error code otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_tcal_update_tsc(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_tsc *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_tcal_header_tsc(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_tsc *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_tcal_update_cbr(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_cbr *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_tcal_header_cbr(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_cbr *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_tcal_update_tma(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_tma *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_tcal_update_mtc(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_mtc *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
extern int pt_tcal_update_cyc(struct pt_time_cal *,
Packit b1f7ae
			      const struct pt_packet_cyc *,
Packit b1f7ae
			      const struct pt_config *);
Packit b1f7ae
Packit b1f7ae
#endif /* PT_TIME_H */