summaryrefslogtreecommitdiffstats
path: root/xlators/debug
diff options
context:
space:
mode:
authorKaleb S. KEITHLEY <kkeithle@redhat.com>2012-06-13 09:13:04 -0400
committerAnand Avati <avati@redhat.com>2012-06-29 14:08:14 -0700
commit5672e77d3102a990a2aa11e7e56ebfe6a0eee369 (patch)
treec92612a433383c70a9e4557ddaffbcdc348e0921 /xlators/debug
parentd87bd36040128c6553e8ee06a363eeb60d16e72c (diff)
localtime and ctime are not MT-SAFE
There are a number of nit-level issues throughout the source with the use of localtime and ctime. While they apparently aren't causing too many problems, apart from the one in bz 828058, they ought to be fixed. Among the "real" problems that are fixed in this patch: 1) general localtime and ctime not MT-SAFE. There's a non-zero chance that another thread calling localtime (or ctime) will over-write the static data about to be used in another thread 2) localtime(& <64-bit-type>) or ctime(& <64-bit-type>) generally not a problem on 64-bit or little-endian 32-bit. But even though we probably have zero users on big-ending 32-bit platforms, it's still incorrect. 3) multiple nested calls passed as params. Last one wins, i.e. over- writes result of prior calls. 4) Inconsistent error handling. Most of these calls are for logging, tracing, or dumping. I submit that if an error somehow occurs in the call to localtime or ctime, the log/trace/dump still should still occur. 5) Appliances should all have their clocks set to UTC, and all log entries, traces, and dumps should use GMT. 6) fix strtok(), change to strtok_r() Other things this patch fixes/changes (that aren't bugs per se): 1) Change "%Y-%m-%d %H:%M:%S" and similar to their equivalent shorthand, e.g. "%F %T" 2) change sizeof(timestr) to sizeof timestr. sizeof is an operator, not a function. You don't use i +(32), why use sizeof(<var>). (And yes, you do use parens with sizeof(<type>).) 3) change 'char timestr[256]' to 'char timestr[32]' where appropriate. Per-thread stack is limited. Time strings are never longer than ~20 characters, so why waste 220+ bytes on the stack? Things this patch doesn't fix: 1) hodgepodge of %Y-%m-%d %H:%M:%S versus %Y/%m/%d-%H%M%S and other variations. It's not clear to me whether this ever matters, not to mention 3rd party log filtering tools may already rely on a particular format. Still it would be nice to have a single manifest constant and have every call to localtime/strftime consistently use the same format. Change-Id: I827cad7bf53e57b69c0173f67abe72884249c1a9 BUG: 832173 Signed-off-by: Kaleb S. KEITHLEY <kkeithle@redhat.com> Reviewed-on: http://review.gluster.com/3568 Tested-by: Gluster Build System <jenkins@build.gluster.com> Reviewed-by: Anand Avati <avati@redhat.com>
Diffstat (limited to 'xlators/debug')
-rw-r--r--xlators/debug/io-stats/src/io-stats.c29
-rw-r--r--xlators/debug/trace/src/trace.c58
2 files changed, 33 insertions, 54 deletions
diff --git a/xlators/debug/io-stats/src/io-stats.c b/xlators/debug/io-stats/src/io-stats.c
index bb6151e..2cf28f1 100644
--- a/xlators/debug/io-stats/src/io-stats.c
+++ b/xlators/debug/io-stats/src/io-stats.c
@@ -568,19 +568,16 @@ ios_dump_throughput_stats (struct ios_stat_head *list_head, xlator_t *this,
FILE* logfp, ios_stats_type_t type)
{
struct ios_stat_list *entry = NULL;
- struct timeval time = {0, };
- struct tm *tm = NULL;
+ struct timeval time = {0, };
char timestr[256] = {0, };
LOCK (&list_head->lock);
{
list_for_each_entry (entry, &list_head->iosstats->list, list) {
- time = entry->iosstat->thru_counters[type].time;
- tm = localtime (&time.tv_sec);
- if (!tm)
- continue;
- strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
- snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
+ gf_time_fmt (timestr, sizeof timestr,
+ entry->iosstat->thru_counters[type].time.tv_sec,
+ gf_timefmt_FT);
+ snprintf (timestr + strlen (timestr), sizeof timestr - strlen (timestr),
".%"GF_PRI_SUSECONDS, time.tv_usec);
ios_log (this, logfp, "%s \t %-10.2f \t %s",
@@ -600,7 +597,6 @@ io_stats_dump_global_to_logfp (xlator_t *this, struct ios_global_stats *stats,
int index = 0;
struct ios_stat_head *list_head = NULL;
struct ios_conf *conf = NULL;
- struct tm *tm = NULL;
char timestr[256] = {0, };
char str_header[128] = {0};
char str_read[128] = {0};
@@ -694,9 +690,10 @@ io_stats_dump_global_to_logfp (xlator_t *this, struct ios_global_stats *stats,
if (interval == -1) {
LOCK (&conf->lock);
{
- tm = localtime (&conf->cumulative.max_openfd_time.tv_sec);
- strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
- snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
+ gf_time_fmt (timestr, sizeof timestr,
+ conf->cumulative.max_openfd_time.tv_sec,
+ gf_timefmt_FT);
+ snprintf (timestr + strlen (timestr), sizeof timestr - strlen (timestr),
".%"GF_PRI_SUSECONDS,
conf->cumulative.max_openfd_time.tv_usec);
ios_log (this, logfp, "Current open fd's: %"PRId64
@@ -1080,7 +1077,6 @@ io_stats_dump_stats_to_dict (xlator_t *this, dict_t *resp,
struct ios_stat_list *entry = NULL;
int ret = -1;
ios_stats_thru_t index = IOS_STATS_THRU_MAX;
- struct tm *tm = NULL;
char timestr[256] = {0, };
char *dict_timestr = NULL;
@@ -1098,9 +1094,10 @@ io_stats_dump_stats_to_dict (xlator_t *this, dict_t *resp,
ret = dict_set_uint64 (resp, "max-open",
conf->cumulative.max_nr_opens);
- tm = localtime (&conf->cumulative.max_openfd_time.tv_sec);
- strftime (timestr, 256, "%Y-%m-%d %H:%M:%S", tm);
- snprintf (timestr + strlen (timestr), 256 - strlen (timestr),
+ gf_time_fmt (timestr, sizeof timestr,
+ conf->cumulative.max_openfd_time.tv_sec,
+ gf_timefmt_FT);
+ snprintf (timestr + strlen (timestr), sizeof timestr - strlen (timestr),
".%"GF_PRI_SUSECONDS,
conf->cumulative.max_openfd_time.tv_usec);
diff --git a/xlators/debug/trace/src/trace.c b/xlators/debug/trace/src/trace.c
index d9c292c..a1136a2 100644
--- a/xlators/debug/trace/src/trace.c
+++ b/xlators/debug/trace/src/trace.c
@@ -46,30 +46,20 @@ int trace_log_level = GF_LOG_INFO;
static char *
trace_stat_to_str (struct iatt *buf)
{
- char *statstr = NULL;
- char atime_buf[256] = {0,};
- char mtime_buf[256] = {0,};
- char ctime_buf[256] = {0,};
- int asprint_ret_value = 0;
- uint64_t ia_time = 0;
+ char *statstr = NULL;
+ char atime_buf[64] = {0,};
+ char mtime_buf[64] = {0,};
+ char ctime_buf[64] = {0,};
+ int asprint_ret_value = 0;
if (!buf) {
statstr = NULL;
goto out;
}
- ia_time = buf->ia_atime;
- strftime (atime_buf, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
-
- ia_time = buf->ia_mtime;
- strftime (mtime_buf, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
-
- ia_time = buf->ia_ctime;
- strftime (ctime_buf, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
-
+ gf_time_fmt (atime_buf, sizeof atime_buf, buf->ia_atime, gf_timefmt_bdT);
+ gf_time_fmt (mtime_buf, sizeof mtime_buf, buf->ia_mtime, gf_timefmt_bdT);
+ gf_time_fmt (ctime_buf, sizeof ctime_buf, buf->ia_ctime, gf_timefmt_bdT);
asprint_ret_value = gf_asprintf (&statstr,
"gfid=%s ino=%"PRIu64", mode=%o, "
"nlink=%"GF_PRI_NLINK", uid=%u, "
@@ -1665,9 +1655,8 @@ int
trace_setattr (call_frame_t *frame, xlator_t *this, loc_t *loc,
struct iatt *stbuf, int32_t valid, dict_t *xdata)
{
- uint64_t ia_time = 0;
- char actime_str[256] = {0,};
- char modtime_str[256] = {0,};
+ char actime_str[64] = {0,};
+ char modtime_str[64] = {0,};
if (trace_fop_names[GF_FOP_SETATTR].enabled) {
if (valid & GF_SET_ATTR_MODE) {
@@ -1685,13 +1674,10 @@ trace_setattr (call_frame_t *frame, xlator_t *this, loc_t *loc,
}
if (valid & (GF_SET_ATTR_ATIME | GF_SET_ATTR_MTIME)) {
- ia_time = stbuf->ia_atime;
- strftime (actime_str, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
-
- ia_time = stbuf->ia_mtime;
- strftime (modtime_str, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
+ gf_time_fmt (actime_str, sizeof actime_str,
+ stbuf->ia_atime, gf_timefmt_bdT);
+ gf_time_fmt (modtime_str, sizeof modtime_str,
+ stbuf->ia_mtime, gf_timefmt_bdT);
gf_log (this->name, GF_LOG_INFO,
"%"PRId64": gfid=%s path=%s ia_atime=%s, ia_mtime=%s",
@@ -1714,9 +1700,8 @@ int
trace_fsetattr (call_frame_t *frame, xlator_t *this, fd_t *fd,
struct iatt *stbuf, int32_t valid, dict_t *xdata)
{
- uint64_t ia_time = 0;
- char actime_str[256] = {0,};
- char modtime_str[256] = {0,};
+ char actime_str[64] = {0,};
+ char modtime_str[64] = {0,};
if (trace_fop_names[GF_FOP_FSETATTR].enabled) {
if (valid & GF_SET_ATTR_MODE) {
@@ -1734,13 +1719,10 @@ trace_fsetattr (call_frame_t *frame, xlator_t *this, fd_t *fd,
}
if (valid & (GF_SET_ATTR_ATIME | GF_SET_ATTR_MTIME)) {
- ia_time = stbuf->ia_atime;
- strftime (actime_str, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
-
- ia_time = stbuf->ia_mtime;
- strftime (modtime_str, 256, "[%b %d %H:%M:%S]",
- localtime ((time_t *)&ia_time));
+ gf_time_fmt (actime_str, sizeof actime_str,
+ stbuf->ia_atime, gf_timefmt_bdT);
+ gf_time_fmt (modtime_str, sizeof modtime_str,
+ stbuf->ia_mtime, gf_timefmt_bdT);
gf_log (this->name, GF_LOG_INFO,
"%"PRId64": gfid=%s fd=%p ia_atime=%s, ia_mtime=%s",