---
name: "sg_get_disk_io_stats"
section: "3"
platform: "linux"
package: "libstatgrab"
version: "0.92.1-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sg_get_disk_io_stats"
---

# sg\_get\_disk\_io\_stats(3)

get disk io statistics

## NAME

sg\_get\_disk\_io\_stats, sg\_get\_disk\_io\_stats\_r, sg\_get\_disk\_io\_stats\_diff, sg\_get\_disk\_io\_stats\_diff\_between, sg\_free\_disk\_io\_stats, sg\_disk\_io\_compare\_name, sg\_disk\_io\_compare\_traffic - get disk io statistics

## SYNOPSIS

```
FC#include <statgrab.h>F[0]
```

FCsg\_disk\_io\_stats \***sg\_get\_disk\_io\_stats**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_disk\_io\_stats \***sg\_get\_disk\_io\_stats\_r**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_disk\_io\_stats \***sg\_get\_disk\_io\_stats\_diff**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_disk\_io\_stats \***sg\_get\_disk\_io\_stats\_diff\_between**F\[0\]  FC(const sg\_disk\_io\_stats \**cur*, const sg\_disk\_io\_stats \**last*, size\_t \**entries*);F\[0\]

FCsg\_error **sg\_free\_disk\_io\_stats**F\[0\]  FC(sg\_disk\_io\_stats \**data*);F\[0\]

FCint **sg\_disk\_io\_compare\_name**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_disk\_io\_compare\_traffic**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

## DESCRIPTION

The FC**sg\_get\_disk\_io\_stats**F\[0\] functions provide disk I/O statistics on a per disk basis. All get- and diff-functions take an optional FCentriesF\[0\] parameter, which points (when given) to a size\_t to take the number of returned vector entries.

The FC**sg\_get\_disk\_io\_stats**F\[0\]() and FC**sg\_get\_disk\_io\_stats\_r**F\[0\]() functions deliver the I/O-statistics since the disk has been attached to the system. The FC**sg\_get\_disk\_io\_stats\_diff**F\[0\]() and FC**sg\_get\_disk\_io\_stats\_diff\_between**F\[0\]() deliver the difference between two calls of FC**sg\_get\_disk\_io\_stats**F\[0\]() or FC**sg\_get\_disk\_io\_stats\_r**F\[0\](), respectively.

**API Shortcut**

| | | |
| --- | --- | --- |
| function | returns | data owner |
| sg\_get\_disk\_io\_stats | sg\_disk\_io\_stats \* | libstatgrab (thread local) |
| sg\_get\_disk\_io\_stats\_r | sg\_disk\_io\_stats \* | caller |
| sg\_get\_disk\_io\_stats\_diff | sg\_disk\_io\_stats \* | libstatgrab (thread local) |
| sg\_get\_disk\_io\_stats\_diff\_between | sg\_disk\_io\_stats \* | caller |

FCsg\_disk\_io\_statsF\[0\] vectors got from FC**sg\_get\_disk\_io\_stats\_r**F\[0\]() or FC**sg\_get\_disk\_io\_stats\_diff\_between**F\[0\]() must be freed using FC**sg\_free\_disk\_io\_stats**F\[0\]() when not needed any more. The caller is responsible for doing it.

Additionally two support functions for FC**qsort**F\[0\](3) are available: FC**sg\_disk\_io\_compare\_name**F\[0\]() and FC**sg\_disk\_io\_compare\_traffic**F\[0\]().

**Example**

```
FC
size_t entries;
sg_disk_io_stats *io_stats = NULL;
while( NULL != ( io_stats = sg_get_disk_io_stats_diff(&entries) ) ) {
    /* show disks with most traffic first */
    qsort( io_stats, entries, sizeof(io_stats[0]), &sg_disk_io_compare_traffic );
    show_disk_io_stats( io_stats );
}
        F[0]
```

On some platforms, such as Solaris 7, the kernel value is stored in a 32bit int, so wraps around when it reaches 4GB. Other platforms, such as Solaris 8 (and most other modern systems), hold the value in a 64bit int, which wraps somewhere near 17 million terabytes. The FC**sg\_get\_disk\_io\_stats\_diff**F\[0\]() function and the FC**sg\_get\_disk\_io\_stats\_diff\_between**F\[0\]() function care about these overflows and try to detect overflows when the diff is calculated.

On Solaris libstatgrab will attempt to get the cXtXdXsX representation for the FCdisk\_nameF\[0\] string. If it fails it will use a name like sd0. On some systems programs calling libstatgrab will need elevated privileges to lookup some of the names. The mappings are built up when FC**sg\_init**F\[0\]() is called for the first time.

## RETURN VALUES

All diskio statistics return a pointer to a structure of type FCsg\_disk\_io\_statsF\[0\].

```
FC
typedef struct {
        char *disk_name;
        unsigned long long read_bytes;
        unsigned long long write_bytes;
        time_t systime;
} sg_disk_io_stats;
    F[0]
```

- FCdisk\_nameF\[0\]

  The name known to the operating system. (eg. on linux it might be hda)

- FCread\_bytesF\[0\]

  The number of bytes the disk has read.

- FCwrite\_bytesF\[0\]

  The number of bytes the disk has written.

- FCsystimeF\[0\]

  The time period over which FCread\_bytesF\[0\] and FCwrite\_bytesF\[0\] were transferred.

## BUGS

FC**sg\_get\_disk\_io\_stats\_diff**F\[0\] and FC**sg\_get\_disk\_io\_stats\_diff\_between**F\[0\] compare two lists of disk (block device) related I/O statistics. Each entry occurring only in the second list is passed through to the resulting list as if it would have been compared to an entry with all statistic values set to 0. This implies, on the very first call FC**sg\_get\_disk\_io\_stats\_diff**F\[0\] will return the same as FC**sg\_get\_disk\_io\_stats**F\[0\].

On operating systems that hold only 32bits of data there is a problem if the values wrap twice. For example, on Solaris 7 if 9GB is transferred and the operating system wraps at 4GB, the FC**sg\_get\_disk\_io\_stats\_diff**F\[0\]() function will return 5GB.

The compare functions exists rather for backward compatibility than for functionality enhancements. Limited flexibility (e.g. reverse order) and lack of optimising opportunities for the compiler leads to the recommendation to implement the required compare routines locally.

## SEE ALSO

[**statgrab**(3)](/statgrab/3.md)

## WEBSITE

⟨https://libstatgrab.org/⟩
