~ubuntu-branches/ubuntu/vivid/sysstat/vivid-proposed

« back to all changes in this revision

Viewing changes to rd_stats.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Luberda
  • Date: 2011-06-15 07:27:46 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110615072746-jqcpj6mlchjuaax9
Tags: 10.0.1-1
* New upstream (stable) release.
* Add Danish translation of debconf templates (closes: #621367).
* Standards-Version: 3.9.2 (no changes).
* sysstat.{postinst,postrm}: register the ucf-managed file with ucfr.
* Sort dependency fields with wrap-and-sort command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1744
1744
 
1745
1745
/*
1746
1746
 ***************************************************************************
 
1747
 * Read current USB device data.
 
1748
 *
 
1749
 * IN:
 
1750
 * @st_pwr_usb          Structure where stats will be saved.
 
1751
 * @usb_device          File name for current USB device.
 
1752
 *
 
1753
 * OUT:
 
1754
 * @st_pwr_usb          Structure with statistics.
 
1755
 ***************************************************************************
 
1756
 */
 
1757
void read_usb_stats(struct stats_pwr_usb *st_pwr_usb, char *usb_device)
 
1758
{
 
1759
        int l;
 
1760
        FILE *fp;
 
1761
        char filename[MAX_PF_NAME];
 
1762
 
 
1763
        /* Get USB device bus number */
 
1764
        sscanf(usb_device, "%u", &st_pwr_usb->bus_nr);
 
1765
 
 
1766
        /* Read USB device vendor ID */
 
1767
        snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
 
1768
                 SYSFS_USBDEV, usb_device, SYSFS_IDVENDOR);
 
1769
        if ((fp = fopen(filename, "r")) != NULL) {
 
1770
                fscanf(fp, "%x",
 
1771
                       &st_pwr_usb->vendor_id);
 
1772
                fclose(fp);
 
1773
        }
 
1774
 
 
1775
        /* Read USB device product ID */
 
1776
        snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
 
1777
                 SYSFS_USBDEV, usb_device, SYSFS_IDPRODUCT);
 
1778
        if ((fp = fopen(filename, "r")) != NULL) {
 
1779
                fscanf(fp, "%x",
 
1780
                       &st_pwr_usb->product_id);
 
1781
                fclose(fp);
 
1782
        }
 
1783
        
 
1784
        /* Read USB device max power consumption */
 
1785
        snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
 
1786
                 SYSFS_USBDEV, usb_device, SYSFS_BMAXPOWER);
 
1787
        if ((fp = fopen(filename, "r")) != NULL) {
 
1788
                fscanf(fp, "%u",
 
1789
                       &st_pwr_usb->bmaxpower);
 
1790
                fclose(fp);
 
1791
        }
 
1792
 
 
1793
        /* Read USB device manufacturer */
 
1794
        snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
 
1795
                 SYSFS_USBDEV, usb_device, SYSFS_MANUFACTURER);
 
1796
        if ((fp = fopen(filename, "r")) != NULL) {
 
1797
                fgets(st_pwr_usb->manufacturer, MAX_MANUF_LEN - 1, fp);
 
1798
                fclose(fp);
 
1799
                if ((l = strlen(st_pwr_usb->manufacturer)) > 0) {
 
1800
                        /* Remove trailing CR */
 
1801
                        st_pwr_usb->manufacturer[l - 1] = '\0';
 
1802
                }
 
1803
        }
 
1804
 
 
1805
        /* Read USB device product */
 
1806
        snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
 
1807
                 SYSFS_USBDEV, usb_device, SYSFS_PRODUCT);
 
1808
        if ((fp = fopen(filename, "r")) != NULL) {
 
1809
                fgets(st_pwr_usb->product, MAX_PROD_LEN - 1, fp);
 
1810
                fclose(fp);
 
1811
                if ((l = strlen(st_pwr_usb->product)) > 0) {
 
1812
                        /* Remove trailing CR */
 
1813
                        st_pwr_usb->product[l - 1] = '\0';
 
1814
                }
 
1815
        }
 
1816
}
 
1817
 
 
1818
/*
 
1819
 ***************************************************************************
 
1820
 * Read USB devices statistics.
 
1821
 *
 
1822
 * IN:
 
1823
 * @st_pwr_usb          Structure where stats will be saved.
 
1824
 * @nbr                 Total number of USB devices.
 
1825
 *
 
1826
 * OUT:
 
1827
 * @st_pwr_usb          Structure with statistics.
 
1828
 ***************************************************************************
 
1829
 */
 
1830
void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
 
1831
{
 
1832
        DIR *dir;
 
1833
        struct dirent *drd;
 
1834
        struct stats_pwr_usb *st_pwr_usb_j;
 
1835
        int j = 0;
 
1836
 
 
1837
        /* Open relevant /sys directory */
 
1838
        if ((dir = opendir(SYSFS_USBDEV)) == NULL)
 
1839
                return;
 
1840
 
 
1841
        /* Get current file entry */
 
1842
        while ((drd = readdir(dir)) != NULL) {
 
1843
 
 
1844
                if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
 
1845
                        if (j < nbr) {
 
1846
                                /* Read current USB device data */
 
1847
                                st_pwr_usb_j = st_pwr_usb + j;
 
1848
                                read_usb_stats(st_pwr_usb_j, drd->d_name);
 
1849
                                j++;
 
1850
                        }
 
1851
                        else
 
1852
                                break;
 
1853
                }
 
1854
        }
 
1855
 
 
1856
        /* Close directory */
 
1857
        closedir(dir);
 
1858
}
 
1859
 
 
1860
/*
 
1861
 ***************************************************************************
1747
1862
 * Read machine uptime, independently of the number of processors.
1748
1863
 *
1749
1864
 * OUT:
1759
1874
        if ((fp = fopen(UPTIME, "r")) == NULL)
1760
1875
                return;
1761
1876
 
1762
 
        if (fgets(line, 128, fp) == NULL)
 
1877
        if (fgets(line, 128, fp) == NULL) {
 
1878
                fclose(fp);
1763
1879
                return;
 
1880
        }
1764
1881
 
1765
1882
        sscanf(line, "%lu.%lu", &up_sec, &up_cent);
1766
1883
        *uptime = (unsigned long long) up_sec * HZ +
2125
2242
 
2126
2243
        return freq;
2127
2244
}
 
2245
 
 
2246
/*
 
2247
 ***************************************************************************
 
2248
 * Count number of USB devices in /sys/bus/usb/devices.
 
2249
 *
 
2250
 * RETURNS:
 
2251
 * Number of USB devices plugged into the system.
 
2252
 * Don't count USB root hubs.
 
2253
 * Return -1 if directory doesn't exist in sysfs.
 
2254
 ***************************************************************************
 
2255
 */
 
2256
int get_usb_nr(void)
 
2257
{
 
2258
        DIR *dir;
 
2259
        struct dirent *drd;
 
2260
        int usb = 0;
 
2261
 
 
2262
        /* Open relevant /sys directory */
 
2263
        if ((dir = opendir(SYSFS_USBDEV)) == NULL)
 
2264
                return -1;
 
2265
 
 
2266
        /* Get current file entry */
 
2267
        while ((drd = readdir(dir)) != NULL) {
 
2268
 
 
2269
                if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
 
2270
                        usb++;
 
2271
                }
 
2272
        }
 
2273
 
 
2274
        /* Close directory */
 
2275
        closedir(dir);
 
2276
 
 
2277
        return usb;
 
2278
}