blob: da0979d2d7cb5e3b64d36cba15115f77da40b6ef [file] [log] [blame]
/*
* Copyright 2012 Google Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <sysinfo.h>
#include "base/fwdb.h"
#include "base/init_funcs.h"
#include "base/time.h"
#include "base/timestamp.h"
#include "drivers/timer/timer.h"
static TimestampTable *timestamp_table;
static int timestamp_table_max_entries(TimestampTable *table, size_t size)
{
size_t entries_size = size - offsetof(TimestampTable, entries);
return entries_size / sizeof(TimestampEntry);
}
static void timestamp_table_init(TimestampTable *table, size_t size)
{
table->base_time = 0;
table->max_entries = timestamp_table_max_entries(table, size);
table->num_entries = 0;
}
static int timestamp_table_install(void)
{
if (timestamp_table) {
printf("Timestamp table is already initialized.\n");
return 1;
}
FwdbEntry default_entry = {
.size = offsetof(TimestampTable, entries) +
CONFIG_MAX_TIMESTAMPS * sizeof(TimestampEntry),
};
FwdbEntry entry;
if (fwdb_access("timestamp table", &entry, &default_entry))
return 1;
TimestampTable *table = (TimestampTable *)entry.ptr;
if (!table->max_entries)
timestamp_table_init(table, entry.size);
int max_entries = timestamp_table_max_entries(table, entry.size);
if (table->max_entries > max_entries) {
printf("Timestamp table max entries too high.\n");
return 1;
}
if (table->num_entries > table->max_entries) {
printf("Timestamp table over filled.\n");
return 1;
}
timestamp_table = table;
timestamp_add_now(TS_START);
return 0;
}
INIT_FUNC_TIMESTAMP(timestamp_table_install)
void timestamp_add(TimestampId id, uint64_t time)
{
TimestampEntry *tse;
if (!timestamp_table ||
(timestamp_table->num_entries ==
timestamp_table->max_entries)) {
return;
}
tse = &timestamp_table->entries[timestamp_table->num_entries++];
tse->id = id;
tse->stamp = time - timestamp_table->base_time;
}
void timestamp_add_now(TimestampId id)
{
if (CONFIG_TIMESTAMP_RAW)
timestamp_add(id, timer_raw_value());
else
timestamp_add(id, time_us(0));
}