diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/perf_counter.c | 338 |
1 files changed, 236 insertions, 102 deletions
diff --git a/kernel/perf_counter.c b/kernel/perf_counter.c index b0b20a07f394..534e20d14d63 100644 --- a/kernel/perf_counter.c +++ b/kernel/perf_counter.c | |||
| @@ -88,6 +88,7 @@ void __weak hw_perf_disable(void) { barrier(); } | |||
| 88 | void __weak hw_perf_enable(void) { barrier(); } | 88 | void __weak hw_perf_enable(void) { barrier(); } |
| 89 | 89 | ||
| 90 | void __weak hw_perf_counter_setup(int cpu) { barrier(); } | 90 | void __weak hw_perf_counter_setup(int cpu) { barrier(); } |
| 91 | void __weak hw_perf_counter_setup_online(int cpu) { barrier(); } | ||
| 91 | 92 | ||
| 92 | int __weak | 93 | int __weak |
| 93 | hw_perf_group_sched_in(struct perf_counter *group_leader, | 94 | hw_perf_group_sched_in(struct perf_counter *group_leader, |
| @@ -306,6 +307,10 @@ counter_sched_out(struct perf_counter *counter, | |||
| 306 | return; | 307 | return; |
| 307 | 308 | ||
| 308 | counter->state = PERF_COUNTER_STATE_INACTIVE; | 309 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 310 | if (counter->pending_disable) { | ||
| 311 | counter->pending_disable = 0; | ||
| 312 | counter->state = PERF_COUNTER_STATE_OFF; | ||
| 313 | } | ||
| 309 | counter->tstamp_stopped = ctx->time; | 314 | counter->tstamp_stopped = ctx->time; |
| 310 | counter->pmu->disable(counter); | 315 | counter->pmu->disable(counter); |
| 311 | counter->oncpu = -1; | 316 | counter->oncpu = -1; |
| @@ -1691,7 +1696,32 @@ static int perf_release(struct inode *inode, struct file *file) | |||
| 1691 | return 0; | 1696 | return 0; |
| 1692 | } | 1697 | } |
| 1693 | 1698 | ||
| 1694 | static u64 perf_counter_read_tree(struct perf_counter *counter) | 1699 | static int perf_counter_read_size(struct perf_counter *counter) |
| 1700 | { | ||
| 1701 | int entry = sizeof(u64); /* value */ | ||
| 1702 | int size = 0; | ||
| 1703 | int nr = 1; | ||
| 1704 | |||
| 1705 | if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | ||
| 1706 | size += sizeof(u64); | ||
| 1707 | |||
| 1708 | if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | ||
| 1709 | size += sizeof(u64); | ||
| 1710 | |||
| 1711 | if (counter->attr.read_format & PERF_FORMAT_ID) | ||
| 1712 | entry += sizeof(u64); | ||
| 1713 | |||
| 1714 | if (counter->attr.read_format & PERF_FORMAT_GROUP) { | ||
| 1715 | nr += counter->group_leader->nr_siblings; | ||
| 1716 | size += sizeof(u64); | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | size += entry * nr; | ||
| 1720 | |||
| 1721 | return size; | ||
| 1722 | } | ||
| 1723 | |||
| 1724 | static u64 perf_counter_read_value(struct perf_counter *counter) | ||
| 1695 | { | 1725 | { |
| 1696 | struct perf_counter *child; | 1726 | struct perf_counter *child; |
| 1697 | u64 total = 0; | 1727 | u64 total = 0; |
| @@ -1703,14 +1733,96 @@ static u64 perf_counter_read_tree(struct perf_counter *counter) | |||
| 1703 | return total; | 1733 | return total; |
| 1704 | } | 1734 | } |
| 1705 | 1735 | ||
| 1736 | static int perf_counter_read_entry(struct perf_counter *counter, | ||
| 1737 | u64 read_format, char __user *buf) | ||
| 1738 | { | ||
| 1739 | int n = 0, count = 0; | ||
| 1740 | u64 values[2]; | ||
| 1741 | |||
| 1742 | values[n++] = perf_counter_read_value(counter); | ||
| 1743 | if (read_format & PERF_FORMAT_ID) | ||
| 1744 | values[n++] = primary_counter_id(counter); | ||
| 1745 | |||
| 1746 | count = n * sizeof(u64); | ||
| 1747 | |||
| 1748 | if (copy_to_user(buf, values, count)) | ||
| 1749 | return -EFAULT; | ||
| 1750 | |||
| 1751 | return count; | ||
| 1752 | } | ||
| 1753 | |||
| 1754 | static int perf_counter_read_group(struct perf_counter *counter, | ||
| 1755 | u64 read_format, char __user *buf) | ||
| 1756 | { | ||
| 1757 | struct perf_counter *leader = counter->group_leader, *sub; | ||
| 1758 | int n = 0, size = 0, err = -EFAULT; | ||
| 1759 | u64 values[3]; | ||
| 1760 | |||
| 1761 | values[n++] = 1 + leader->nr_siblings; | ||
| 1762 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | ||
| 1763 | values[n++] = leader->total_time_enabled + | ||
| 1764 | atomic64_read(&leader->child_total_time_enabled); | ||
| 1765 | } | ||
| 1766 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | ||
| 1767 | values[n++] = leader->total_time_running + | ||
| 1768 | atomic64_read(&leader->child_total_time_running); | ||
| 1769 | } | ||
| 1770 | |||
| 1771 | size = n * sizeof(u64); | ||
| 1772 | |||
| 1773 | if (copy_to_user(buf, values, size)) | ||
| 1774 | return -EFAULT; | ||
| 1775 | |||
| 1776 | err = perf_counter_read_entry(leader, read_format, buf + size); | ||
| 1777 | if (err < 0) | ||
| 1778 | return err; | ||
| 1779 | |||
| 1780 | size += err; | ||
| 1781 | |||
| 1782 | list_for_each_entry(sub, &leader->sibling_list, list_entry) { | ||
| 1783 | err = perf_counter_read_entry(counter, read_format, | ||
| 1784 | buf + size); | ||
| 1785 | if (err < 0) | ||
| 1786 | return err; | ||
| 1787 | |||
| 1788 | size += err; | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | return size; | ||
| 1792 | } | ||
| 1793 | |||
| 1794 | static int perf_counter_read_one(struct perf_counter *counter, | ||
| 1795 | u64 read_format, char __user *buf) | ||
| 1796 | { | ||
| 1797 | u64 values[4]; | ||
| 1798 | int n = 0; | ||
| 1799 | |||
| 1800 | values[n++] = perf_counter_read_value(counter); | ||
| 1801 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { | ||
| 1802 | values[n++] = counter->total_time_enabled + | ||
| 1803 | atomic64_read(&counter->child_total_time_enabled); | ||
| 1804 | } | ||
| 1805 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { | ||
| 1806 | values[n++] = counter->total_time_running + | ||
| 1807 | atomic64_read(&counter->child_total_time_running); | ||
| 1808 | } | ||
| 1809 | if (read_format & PERF_FORMAT_ID) | ||
| 1810 | values[n++] = primary_counter_id(counter); | ||
| 1811 | |||
| 1812 | if (copy_to_user(buf, values, n * sizeof(u64))) | ||
| 1813 | return -EFAULT; | ||
| 1814 | |||
| 1815 | return n * sizeof(u64); | ||
| 1816 | } | ||
| 1817 | |||
| 1706 | /* | 1818 | /* |
| 1707 | * Read the performance counter - simple non blocking version for now | 1819 | * Read the performance counter - simple non blocking version for now |
| 1708 | */ | 1820 | */ |
| 1709 | static ssize_t | 1821 | static ssize_t |
| 1710 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) | 1822 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) |
| 1711 | { | 1823 | { |
| 1712 | u64 values[4]; | 1824 | u64 read_format = counter->attr.read_format; |
| 1713 | int n; | 1825 | int ret; |
| 1714 | 1826 | ||
| 1715 | /* | 1827 | /* |
| 1716 | * Return end-of-file for a read on a counter that is in | 1828 | * Return end-of-file for a read on a counter that is in |
| @@ -1720,28 +1832,18 @@ perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) | |||
| 1720 | if (counter->state == PERF_COUNTER_STATE_ERROR) | 1832 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 1721 | return 0; | 1833 | return 0; |
| 1722 | 1834 | ||
| 1835 | if (count < perf_counter_read_size(counter)) | ||
| 1836 | return -ENOSPC; | ||
| 1837 | |||
| 1723 | WARN_ON_ONCE(counter->ctx->parent_ctx); | 1838 | WARN_ON_ONCE(counter->ctx->parent_ctx); |
| 1724 | mutex_lock(&counter->child_mutex); | 1839 | mutex_lock(&counter->child_mutex); |
| 1725 | values[0] = perf_counter_read_tree(counter); | 1840 | if (read_format & PERF_FORMAT_GROUP) |
| 1726 | n = 1; | 1841 | ret = perf_counter_read_group(counter, read_format, buf); |
| 1727 | if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | 1842 | else |
| 1728 | values[n++] = counter->total_time_enabled + | 1843 | ret = perf_counter_read_one(counter, read_format, buf); |
| 1729 | atomic64_read(&counter->child_total_time_enabled); | ||
| 1730 | if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | ||
| 1731 | values[n++] = counter->total_time_running + | ||
| 1732 | atomic64_read(&counter->child_total_time_running); | ||
| 1733 | if (counter->attr.read_format & PERF_FORMAT_ID) | ||
| 1734 | values[n++] = primary_counter_id(counter); | ||
| 1735 | mutex_unlock(&counter->child_mutex); | 1844 | mutex_unlock(&counter->child_mutex); |
| 1736 | 1845 | ||
| 1737 | if (count < n * sizeof(u64)) | 1846 | return ret; |
| 1738 | return -EINVAL; | ||
| 1739 | count = n * sizeof(u64); | ||
| 1740 | |||
| 1741 | if (copy_to_user(buf, values, count)) | ||
| 1742 | return -EFAULT; | ||
| 1743 | |||
| 1744 | return count; | ||
| 1745 | } | 1847 | } |
| 1746 | |||
