+#if OSD_THANDLE_STATS
+/**
+ * Set time when the handle is allocated
+ */
+static void osd_th_alloced(struct osd_thandle *oth)
+{
+ oth->oth_alloced = cfs_time_current();
+}
+
+/**
+ * Set time when the handle started
+ */
+static void osd_th_started(struct osd_thandle *oth)
+{
+ oth->oth_started = cfs_time_current();
+}
+
+/**
+ * Helper function to convert time interval to microseconds packed in
+ * long int (default time units for the counter in "stats" initialized
+ * by lu_time_init() )
+ */
+static long interval_to_usec(cfs_time_t start, cfs_time_t end)
+{
+ struct timeval val;
+
+ cfs_duration_usec(cfs_time_sub(end, start), &val);
+ return val.tv_sec * 1000000 + val.tv_usec;
+}
+
+/**
+ * Check whether the we deal with this handle for too long.
+ */
+static void __osd_th_check_slow(void *oth, struct osd_device *dev,
+ cfs_time_t alloced, cfs_time_t started,
+ cfs_time_t closed)
+{
+ cfs_time_t now = cfs_time_current();
+
+ LASSERT(dev != NULL);
+
+ lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
+ interval_to_usec(alloced, started));
+ lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
+ interval_to_usec(started, closed));
+ lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
+ interval_to_usec(closed, now));
+
+ if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
+ CWARN("transaction handle %p was open for too long: "
+ "now "CFS_TIME_T" ,"
+ "alloced "CFS_TIME_T" ,"
+ "started "CFS_TIME_T" ,"
+ "closed "CFS_TIME_T"\n",
+ oth, now, alloced, started, closed);
+ libcfs_debug_dumpstack(NULL);
+ }
+}
+
+#define OSD_CHECK_SLOW_TH(oth, dev, expr) \
+{ \
+ cfs_time_t __closed = cfs_time_current(); \
+ cfs_time_t __alloced = oth->oth_alloced; \
+ cfs_time_t __started = oth->oth_started; \
+ \
+ expr; \
+ __osd_th_check_slow(oth, dev, __alloced, __started, __closed); \
+}
+
+#else /* OSD_THANDLE_STATS */
+
+#define osd_th_alloced(h) do {} while(0)
+#define osd_th_started(h) do {} while(0)
+#define OSD_CHECK_SLOW_TH(oth, dev, expr) expr
+
+#endif /* OSD_THANDLE_STATS */
+