patch 4.0
This commit is contained in:
90
deps/oblib/src/lib/signal/ob_signal_utils.cpp
vendored
90
deps/oblib/src/lib/signal/ob_signal_utils.cpp
vendored
@ -20,10 +20,12 @@
|
||||
#include "lib/charset/ob_mysql_global.h"
|
||||
#include "lib/signal/ob_libunwind.h"
|
||||
|
||||
namespace oceanbase {
|
||||
namespace common {
|
||||
namespace oceanbase
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
int g_log_level_ = DLogLevel::INFO;
|
||||
__thread bool LoggerSwitchGuard::g_logger_open_ = true;
|
||||
_RLOCAL(bool, LoggerSwitchGuard::g_logger_open_);
|
||||
|
||||
/* From mongodb */
|
||||
void safe_sleep_micros(int64_t usec)
|
||||
@ -34,30 +36,35 @@ void safe_sleep_micros(int64_t usec)
|
||||
nanosleep(&ts, nullptr);
|
||||
}
|
||||
|
||||
__thread ObJumpBuf* g_jmp = nullptr;
|
||||
_RLOCAL(ObJumpBuf *, g_jmp);
|
||||
_RLOCAL(ByteBuf<256>, crash_restore_buffer);
|
||||
|
||||
void crash_restore_handler(int sig, siginfo_t* s, void* p)
|
||||
void crash_restore_handler(int sig, siginfo_t *s, void *p)
|
||||
{
|
||||
if (SIGSEGV == sig || SIGABRT == sig || SIGBUS == sig || SIGFPE == sig) {
|
||||
if (SIGSEGV == sig || SIGABRT == sig ||
|
||||
SIGBUS == sig || SIGFPE == sig) {
|
||||
int64_t len = 0;
|
||||
safe_backtrace(crash_restore_buffer, 255, &len);
|
||||
crash_restore_buffer[len++] = '\0';
|
||||
siglongjmp(*g_jmp, 1);
|
||||
} else {
|
||||
ob_signal_handler(sig, s, p);
|
||||
}
|
||||
}
|
||||
|
||||
#define LEAPOCH (946684800LL + 86400 * (31 + 29))
|
||||
#define DAYS_PER_400Y (365 * 400 + 97)
|
||||
#define DAYS_PER_100Y (365 * 100 + 24)
|
||||
#define DAYS_PER_4Y (365 * 4 + 1)
|
||||
#define LEAPOCH (946684800LL + 86400*(31+29))
|
||||
#define DAYS_PER_400Y (365*400 + 97)
|
||||
#define DAYS_PER_100Y (365*100 + 24)
|
||||
#define DAYS_PER_4Y (365*4 + 1)
|
||||
|
||||
int safe_secs_to_tm(long long t, struct tm* tm)
|
||||
int safe_secs_to_tm(long long t, struct tm *tm)
|
||||
{
|
||||
long long days, secs;
|
||||
int remdays, remsecs, remyears;
|
||||
int qc_cycles, c_cycles, q_cycles;
|
||||
int years, months;
|
||||
int wday, yday, leap;
|
||||
static const char days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29};
|
||||
static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
|
||||
|
||||
if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
|
||||
return -1;
|
||||
@ -70,9 +77,8 @@ int safe_secs_to_tm(long long t, struct tm* tm)
|
||||
days--;
|
||||
}
|
||||
|
||||
wday = (3 + days) % 7;
|
||||
if (wday < 0)
|
||||
wday += 7;
|
||||
wday = (3+days)%7;
|
||||
if (wday < 0) wday += 7;
|
||||
|
||||
qc_cycles = days / DAYS_PER_400Y;
|
||||
remdays = days % DAYS_PER_400Y;
|
||||
@ -82,37 +88,33 @@ int safe_secs_to_tm(long long t, struct tm* tm)
|
||||
}
|
||||
|
||||
c_cycles = remdays / DAYS_PER_100Y;
|
||||
if (c_cycles == 4)
|
||||
c_cycles--;
|
||||
if (c_cycles == 4) c_cycles--;
|
||||
remdays -= c_cycles * DAYS_PER_100Y;
|
||||
|
||||
q_cycles = remdays / DAYS_PER_4Y;
|
||||
if (q_cycles == 25)
|
||||
q_cycles--;
|
||||
if (q_cycles == 25) q_cycles--;
|
||||
remdays -= q_cycles * DAYS_PER_4Y;
|
||||
|
||||
remyears = remdays / 365;
|
||||
if (remyears == 4)
|
||||
remyears--;
|
||||
if (remyears == 4) remyears--;
|
||||
remdays -= remyears * 365;
|
||||
|
||||
leap = !remyears && (q_cycles || !c_cycles);
|
||||
yday = remdays + 31 + 28 + leap;
|
||||
if (yday >= 365 + leap)
|
||||
yday -= 365 + leap;
|
||||
if (yday >= 365+leap) yday -= 365+leap;
|
||||
|
||||
years = remyears + 4 * q_cycles + 100 * c_cycles + 400 * qc_cycles;
|
||||
years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles;
|
||||
|
||||
for (months = 0; days_in_month[months] <= remdays; months++)
|
||||
for (months=0; days_in_month[months] <= remdays; months++)
|
||||
remdays -= days_in_month[months];
|
||||
|
||||
if (years + 100 > INT_MAX || years + 100 < INT_MIN)
|
||||
if (years+100 > INT_MAX || years+100 < INT_MIN)
|
||||
return -1;
|
||||
|
||||
tm->tm_year = years + 100;
|
||||
tm->tm_mon = months + 2;
|
||||
if (tm->tm_mon >= 12) {
|
||||
tm->tm_mon -= 12;
|
||||
tm->tm_mon -=12;
|
||||
tm->tm_year++;
|
||||
}
|
||||
tm->tm_mday = remdays + 1;
|
||||
@ -126,44 +128,34 @@ int safe_secs_to_tm(long long t, struct tm* tm)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void safe_current_datetime_str(char* buf, int64_t len, int64_t& pos)
|
||||
void safe_current_datetime_str(char *buf, int64_t len, int64_t &pos)
|
||||
{
|
||||
pos = 0;
|
||||
struct timespec ts = {0, 0};
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
struct tm tm;
|
||||
safe_secs_to_tm(ts.tv_sec, &tm);
|
||||
int64_t count = safe_snprintf(
|
||||
buf, len, "%d%d%d%d%d%d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
int64_t count = safe_snprintf(buf, len, "%d%d%d%d%d%d",
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
if (count >= 0 && count < len) {
|
||||
pos = count;
|
||||
} else {
|
||||
pos = 0; /*overflow*/
|
||||
}
|
||||
} else { pos = 0;/*overflow*/}
|
||||
}
|
||||
|
||||
void safe_current_datetime_str_v2(char* buf, int64_t len, int64_t& pos)
|
||||
void safe_current_datetime_str_v2(char *buf, int64_t len, int64_t &pos)
|
||||
{
|
||||
pos = 0;
|
||||
struct timespec ts = {0, 0};
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
struct tm tm;
|
||||
safe_secs_to_tm(ts.tv_sec, &tm);
|
||||
int64_t count = safe_snprintf(buf,
|
||||
len,
|
||||
"%d-%d-%d %d:%d:%d.%ld",
|
||||
tm.tm_year + 1900,
|
||||
tm.tm_mon + 1,
|
||||
tm.tm_mday,
|
||||
tm.tm_hour,
|
||||
tm.tm_min,
|
||||
tm.tm_sec,
|
||||
(long)(ts.tv_nsec * 1e-3));
|
||||
int64_t count = safe_snprintf(buf, len, "%d-%d-%d %d:%d:%d.%ld",
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)(ts.tv_nsec * 1e-3));
|
||||
if (count >= 0 && count < len) {
|
||||
pos = count;
|
||||
} else {
|
||||
pos = 0; /*overflow*/
|
||||
}
|
||||
} else { pos = 0;/*overflow*/}
|
||||
}
|
||||
|
||||
int wait_readable(int fd, int64_t timeout)
|
||||
@ -190,5 +182,5 @@ int wait_readable(int fd, int64_t timeout)
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace common
|
||||
} // namespace oceanbase
|
||||
} // namespace common
|
||||
} // namespace oceanbase
|
||||
|
||||
Reference in New Issue
Block a user