4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdclass/lu_time.c
36 * Lustre Time Tracking.
37 * These are the only exported functions, they provide some generic
38 * infrastructure for managing object devices.
40 * Author: Nikita Danilov <nikita@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_CLASS
45 #include <obd_class.h>
46 /* OBD_{ALLOC,FREE}_PTR() */
47 #include <obd_support.h>
48 #include <lprocfs_status.h>
49 #include <lu_object.h>
53 LU_TIME_DEPTH_MAX = 16
57 int ltd_tos; /* top of the stack */
58 unsigned long long ltd_timestamp[LU_TIME_DEPTH_MAX];
61 /* context key constructor/destructor: lu_time_key_init, lu_time_key_fini */
62 LU_KEY_INIT_FINI(lu_time, struct lu_time_data);
64 void lu_time_key_exit(const struct lu_context *ctx,
65 struct lu_context_key *key, void *data)
67 struct lu_time_data *value = data;
68 LASSERT(value->ltd_tos == 0);
72 * Key, holding temporary buffer. This key is registered very early by
75 static struct lu_context_key lu_time_key = {
76 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
77 .lct_init = lu_time_key_init,
78 .lct_fini = lu_time_key_fini,
79 .lct_exit = lu_time_key_exit
82 int lu_time_global_init(void)
84 LU_CONTEXT_KEY_INIT(&lu_time_key);
85 return lu_context_key_register(&lu_time_key);
88 void lu_time_global_fini(void)
90 lu_context_key_degister(&lu_time_key);
93 int lu_time_named_init(struct lprocfs_stats **stats, const char *name,
94 cfs_proc_dir_entry_t *entry,
95 const char **names, int nr)
106 *stats = lprocfs_alloc_stats(nr, 0);
107 if (*stats != NULL) {
108 result = lprocfs_register_stats(entry, name, *stats);
110 for (i = 0; i < nr; ++i) {
111 lprocfs_counter_init(*stats, i,
112 LPROCFS_CNTR_AVGMINMAX,
124 EXPORT_SYMBOL(lu_time_named_init);
126 int lu_time_init(struct lprocfs_stats **stats, cfs_proc_dir_entry_t *entry,
127 const char **names, int nr)
129 return lu_time_named_init(stats, "lu_stats", entry, names, nr);
131 EXPORT_SYMBOL(lu_time_init);
133 void lu_time_fini(struct lprocfs_stats **stats)
135 if (*stats != NULL) {
136 lprocfs_free_stats(stats);
140 EXPORT_SYMBOL(lu_time_fini);
142 static inline struct lu_time_data *lu_time_data_get(const struct lu_env *env)
144 return lu_context_key_get(&env->le_ctx, &lu_time_key);
147 int lu_time_is_clean(const struct lu_env *env)
149 return lu_time_data_get(env)->ltd_tos == 0;
151 EXPORT_SYMBOL(lu_time_is_clean);
153 /* from sleepometer by Andrew Morton */
154 unsigned long long lu_time_stamp_get(void)
157 * Return timestamp with microsecond precision. This has to be cheap.
160 #if defined(CONFIG_X86) && !defined(CONFIG_X86_64)
162 * do_gettimeofday() goes backwards sometimes :(. Usethe TSC
164 unsigned long long ret;
167 do_div(ret, cpu_khz / 1000);
171 unsigned long long ret;
173 cfs_gettimeofday(&now);
181 * Export it, but do not advertise in headers. This is limited use only.
183 EXPORT_SYMBOL(lu_time_stamp_get);
185 void lu_lprocfs_time_start(const struct lu_env *env)
187 struct lu_time_data *ltd = lu_time_data_get(env);
189 LASSERT(0 <= ltd->ltd_tos);
190 LASSERT(ltd->ltd_tos < ARRAY_SIZE(ltd->ltd_timestamp));
191 ltd->ltd_timestamp[ltd->ltd_tos++] = lu_time_stamp_get();
193 EXPORT_SYMBOL(lu_lprocfs_time_start);
195 void lu_lprocfs_time_end(const struct lu_env *env,
196 struct lprocfs_stats *stats, int idx)
198 struct lu_time_data *ltd = lu_time_data_get(env);
202 LASSERT(0 <= ltd->ltd_tos);
203 LASSERT(ltd->ltd_tos < ARRAY_SIZE(ltd->ltd_timestamp));
204 diff = lu_time_stamp_get() - ltd->ltd_timestamp[ltd->ltd_tos];
205 if (diff >= 0 && stats != NULL)
206 lprocfs_counter_add(stats, idx, diff);
208 EXPORT_SYMBOL(lu_lprocfs_time_end);