可以在用prink打的log前面加上时间戳,但这个时间戳是怎么得到的呢?
我们知道printk-> vprintk_emit
1608 asmlinkage int vprintk_emit(int facility, int level,
1609 const char *dict, size_t dictlen,
1610 const char *fmt, va_list args)
1611 {
1748 if (cont.len) {
1749 if (cont.owner == current && !(lflags & LOG_PREFIX))
1750 stored = cont_add(facility, level, text,
1751 text_len);
1752 cont_flush(LOG_NEWLINE);
1753 }
1754
1755 if (stored)
1756 printed_len += text_len;
1757 else
1758 printed_len += log_store(facility, level, lflags, 0,
1759 dict, dictlen, text, text_len);
1760 }
1761
这个函数的1748行会判断要打印的log是否为NULL,不为NULL的话,就会走到cont_add 来得到当前log的时间
1546 static bool cont_add(int facility, int level, const char *text, size_t len)
1547 {
1548 if (cont.len && cont.flushed)
1549 return false;
1550
1551 /*
1552 * If ext consoles are present, flush and skip in-kernel
1553 * continuation. See nr_ext_console_drivers definition. Also, if
1554 * the line gets too long, split it up in separate records.
1555 */
1556 if (nr_ext_console_drivers || cont.len + len > sizeof(cont.buf)) {
1557 cont_flush(LOG_CONT);
1558 return false;
1559 }
1560
1561 if (!cont.len) {
1562 cont.facility = facility;
1563 cont.level = level;
1564 cont.owner = current;
1565 cont.ts_nsec = local_clock();
1566 cont.flags = 0;
1567 cont.cons = 0;
1568 cont.flushed = false;
1569 }
1570
1571 memcpy(cont.buf + cont.len, text, len);
1572 cont.len += len;
1573
1574 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1575 cont_flush(LOG_CONT);
1576
1577 return true;
1578 }
这个函数的1665行会得到通过local_clock得到当前时间。在vprintk_emit 也可能是走log_store 这个flow,里面也是调用local_clock 来得到时间。
所以结论就是prink 中的时间戳是通过local_clock得到的.
我们知道printk-> vprintk_emit
1608 asmlinkage int vprintk_emit(int facility, int level,
1609 const char *dict, size_t dictlen,
1610 const char *fmt, va_list args)
1611 {
1748 if (cont.len) {
1749 if (cont.owner == current && !(lflags & LOG_PREFIX))
1750 stored = cont_add(facility, level, text,
1751 text_len);
1752 cont_flush(LOG_NEWLINE);
1753 }
1754
1755 if (stored)
1756 printed_len += text_len;
1757 else
1758 printed_len += log_store(facility, level, lflags, 0,
1759 dict, dictlen, text, text_len);
1760 }
1761
这个函数的1748行会判断要打印的log是否为NULL,不为NULL的话,就会走到cont_add 来得到当前log的时间
1546 static bool cont_add(int facility, int level, const char *text, size_t len)
1547 {
1548 if (cont.len && cont.flushed)
1549 return false;
1550
1551 /*
1552 * If ext consoles are present, flush and skip in-kernel
1553 * continuation. See nr_ext_console_drivers definition. Also, if
1554 * the line gets too long, split it up in separate records.
1555 */
1556 if (nr_ext_console_drivers || cont.len + len > sizeof(cont.buf)) {
1557 cont_flush(LOG_CONT);
1558 return false;
1559 }
1560
1561 if (!cont.len) {
1562 cont.facility = facility;
1563 cont.level = level;
1564 cont.owner = current;
1565 cont.ts_nsec = local_clock();
1566 cont.flags = 0;
1567 cont.cons = 0;
1568 cont.flushed = false;
1569 }
1570
1571 memcpy(cont.buf + cont.len, text, len);
1572 cont.len += len;
1573
1574 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1575 cont_flush(LOG_CONT);
1576
1577 return true;
1578 }
这个函数的1665行会得到通过local_clock得到当前时间。在vprintk_emit 也可能是走log_store 这个flow,里面也是调用local_clock 来得到时间。
所以结论就是prink 中的时间戳是通过local_clock得到的.