Akhil Mohan
Akhil Mohan

Reputation: 105

What is USEC_INITIALIZED property?

I am getting the following output for udevadm info -q property -n /dev/sda

DEVLINKS=/dev/disk/by-path/pci-0000:00:1f.2-ata-1 /dev/disk/by-id/wwn-0x5000c500a90b2880 /dev/disk/by-id/ata-ST500LM021-1KJ152_W62GX4GF
DEVNAME=/dev/sda
DEVPATH=/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
DEVTYPE=disk
ID_ATA=1
ID_ATA_DOWNLOAD_MICROCODE=1
ID_ATA_FEATURE_SET_APM=1
ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=128
ID_ATA_FEATURE_SET_APM_ENABLED=1
ID_ATA_FEATURE_SET_HPA=1
ID_ATA_FEATURE_SET_HPA_ENABLED=1
ID_ATA_FEATURE_SET_PM=1
ID_ATA_FEATURE_SET_PM_ENABLED=1
ID_ATA_FEATURE_SET_PUIS=1
ID_ATA_FEATURE_SET_PUIS_ENABLED=0
ID_ATA_FEATURE_SET_SECURITY=1
ID_ATA_FEATURE_SET_SECURITY_ENABLED=0
ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=78
ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=78
ID_ATA_FEATURE_SET_SMART=1
ID_ATA_FEATURE_SET_SMART_ENABLED=1
ID_ATA_ROTATION_RATE_RPM=7200
ID_ATA_SATA=1
ID_ATA_SATA_SIGNAL_RATE_GEN1=1
ID_ATA_SATA_SIGNAL_RATE_GEN2=1
ID_ATA_WRITE_CACHE=1
ID_ATA_WRITE_CACHE_ENABLED=1
ID_BUS=ata
ID_MODEL=ST500LM021-1KJ152
ID_MODEL_ENC=ST500LM021-1KJ152\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
ID_PART_TABLE_TYPE=gpt
ID_PART_TABLE_UUID=f8f58152-5c2e-4c72-9b1a-8bdde0e1c8ee
ID_PATH=pci-0000:00:1f.2-ata-1
ID_PATH_TAG=pci-0000_00_1f_2-ata-1
ID_REVISION=0005SDM1
ID_SERIAL=ST500LM021-1KJ152_W62GX4GF
ID_SERIAL_SHORT=W62GX4GF
ID_TYPE=disk
ID_WWN=0x5000c500a90b2880
ID_WWN_WITH_EXTENSION=0x5000c500a90b2880
MAJOR=8
MINOR=0
SUBSYSTEM=block
TAGS=:systemd:
USEC_INITIALIZED=2694098

What is the USEC_INITIALIZED property in the last line of the output.

Is it just the time between attaching the device and actual initialization of the device.

Also if it is the initialization time how will the kernel know when the device was initialized as it will be done differently for different types of devices(eg: block device, camera etc).

Upvotes: 2

Views: 2471

Answers (2)

Pavel Piatruk
Pavel Piatruk

Reputation: 61

This code prints UDEV Uptime in seconds

import psutil
import pyudev
from datetime import datetime


def get_udev_uptime_dev(DEV):
    try:
        context = pyudev.Context()
        d=pyudev.Devices.from_path(context, f'/sys/class/net/{DEV}')
        USEC_INITIALIZED=int(d.properties['USEC_INITIALIZED'])
    except Exception as e:
        #print(e)
        return
    if not USEC_INITIALIZED:
        return
    UPTIME_SINCE=psutil.boot_time()
    DEV_SEC_AFTER_UPTIME= int(UPTIME_SINCE + (USEC_INITIALIZED/1000000 ))
    UPTIME_DEV_SEC=int(datetime.now().strftime("%s")) - DEV_SEC_AFTER_UPTIME
    return UPTIME_DEV_SEC

print(get_udev_uptime_dev('modem9'))

Upvotes: 0

Mike Andrews
Mike Andrews

Reputation: 3206

USEC_INITIALIZED is set to the value of the monotonically increasing system clock when the device is registered in udevd. This clock's guarantee is that it never goes backwards. It typically starts at 0 on boot. Here's the function, from systemd/src/libsystemd/sd-device/device-private.c:

int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) {
        usec_t when;

        assert(device);

        if (device_old && device_old->usec_initialized > 0)
                when = device_old->usec_initialized;
        else
                when = now(CLOCK_MONOTONIC);

        return device_set_usec_initialized(device, when);
}

Upvotes: 3

Related Questions