Technische Universität Braunschweig
  • Study & Teaching
    • Beginning your Studies
      • Prospective Students
      • Degree Programmes
      • Application
      • Fit4TU
      • Why Braunschweig?
    • During your Studies
      • Fresher's Hub
      • Term Dates
      • Courses
      • Practical Information
      • Beratungsnavi
      • Additional Qualifications
      • Financing and Costs
      • Special Circumstances
      • Health and Well-being
      • Campus life
    • At the End of your Studies
      • Discontinuation and Credentials Certification
      • After graduation
      • Alumni*ae
    • For Teaching Staff
      • Strategy, Offers and Information
      • Learning Management System Stud.IP
    • Contact
      • Study Service Centre
      • Academic Advice Service
      • Student Office
      • Career Service
  • Research
    • Research Profile
      • Core Research Areas
      • Clusters of Excellence at TU Braunschweig
      • Research Projects
      • Research Centres
      • Professors‘ Research Profiles
    • Early Career Researchers
      • Support in the early stages of an academic career
      • PhD-Students
      • Postdocs
      • Junior research group leaders
      • Junior Professorship and Tenure-Track
      • Habilitation
      • Service Offers for Scientists
    • Research Data & Transparency
      • Transparency in Research
      • Research Data
      • Open Access Strategy
      • Digital Research Announcement
    • Research Funding
      • Research Funding Network
      • Research funding
    • Contact
      • Research Services
      • Academy for Graduates
  • International
    • International Students
      • Why Braunschweig?
      • Degree seeking students
      • Exchange Studies
      • TU Braunschweig Summer School
      • Refugees
      • International Student Support
    • Going Abroad
      • Studying abroad
      • Internships abroad
      • Teaching and research abroad
      • Working abroad
    • International Researchers
      • Welcome Support
      • PhD Studies
      • Service for host institutes
    • Language and intercultural competence training
      • Learning German
      • Learning Foreign Languages
      • Intercultural Communication
    • International Profile
      • Internationalisation
      • International Cooperations
      • Strategic Partnerships
      • International networks
    • International House
      • About us
      • Contact & Office Hours
      • News and Events
      • International Days
      • 5th Student Conference: Internationalisation of Higher Education
      • Newsletter, Podcast & Videos
      • Job Advertisements
  • TU Braunschweig
    • Our Profile
      • Aims & Values
      • Regulations and Guidelines
      • Alliances & Partners
      • The University Development Initiative 2030
      • Foundation University
      • Facts & Figures
      • Our History
    • Career
      • Working at TU Braunschweig
      • Vacancies
    • Economy & Business
      • Entrepreneurship
      • Friends & Supporters
    • General Public
      • Check-in for Students
      • The Student House
      • Access to the University Library
    • Media Services
      • Communications and Press Service
      • Services for media
      • Film and photo permits
      • Advices for scientists
      • Topics and stories
    • Contact
      • General Contact
      • Getting here
  • Organisation
    • Presidency & Administration
      • Executive Board
      • Designated Offices
      • Administration
      • Committees
    • Faculties
      • Carl-Friedrich-Gauß-Fakultät
      • Faculty of Life Sciences
      • Faculty of Architecture, Civil Engineering and Environmental Sciences
      • Faculty of Mechanical Engineering
      • Faculty of Electrical Engineering, Information Technology, Physics
      • Faculty of Humanities and Education
    • Institutes
      • Institutes from A to Z
    • Facilities
      • University Library
      • Gauß-IT-Zentrum
      • Professional and Personnel Development
      • International House
      • The Project House of the TU Braunschweig
      • Transfer Service
      • University Sports Center
      • Facilities from A to Z
    • Equal Opportunity Office
      • Equal Opportunity Office
      • Family
      • Diversity for Students
  • Search
  • Quicklinks
    • People Search
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Cafeteria
    • Courses
    • Stud.IP
    • Library Catalogue
    • IT Services
    • Information Portal (employees)
    • Link Collection
    • DE
    • EN
    • IBR YouTube
    • Facebook
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
Menu
  • Organisation
  • Faculties
  • Carl-Friedrich-Gauß-Fakultät
  • Institutes
  • Institute of Operating Systems and Computer Networks
  • Prof. Dr.-Ing. Christian Dietrich
  • Advent(2)
  • Cookie Counting
Logo IBR
IBR Login
  • Institute of Operating Systems and Computer Networks
    • News
    • About us
      • Whole Team
      • Directions
      • Floor Plan
      • Projects
      • Publications
      • Software
      • News Archive
    • Connected and Mobile Systems
      • Team
      • Courses
      • Theses
      • Projects
      • Publications
      • Software
      • Datasets
    • Reliable System Software
      • Overview
      • Team
      • Teaching
      • Theses & Jobs
      • Research
      • Publications
    • Algorithms
      • Team
      • Courses
      • Theses
      • Projects
      • Publications
    • Microprocessor Lab
    • Education
      • Summer 2025
      • Winter 2024/2025
      • Theses
    • Services
      • Library
      • Mailinglists
      • Webmail
      • Knowledge Base
      • Wiki
      • Account Management
      • Services Status
    • Spin-Offs
      • Docoloc
      • bliq (formerly AIPARK)
      • Confidential Technologies
    • Research Cooperations
      • IST.hub
  • Task Overview
  • Git repository
  • Mailing list
  • Matrix-Channel

Cookie Counting

☃️
Git-Repository: Template Solution Solution-Diff (Solution is posted at 18:00 CET)
Workload: 68 lines of code
Important System-Calls: perf_event_open(2)
Illustration for this exercsie

All cookies are rescued. With your help, the ELF search team was able to find the "borrowing" ELF and bring back all the cookies, which now form a huge, and I mean HUGE, pile of cookies in the Christmas village. But somehow, the pile looks a little bit too small. After some intense eye contact, the borrowing ELF admitted that he ate one or two or maybe a few more cookies on the way. To estimate the number of destroyed cookies, the ELFs came up with two different estimation strategies. As time to Christmas is running out, it is important to choose the faster cookie counting strategy. Can you help?

perf Events

Performing microbenchmarks is an important part of writing efficient software as it helps us to identifying or disprove the bottlenecks in our programs. However, often it is not enough to know how long a given function or component executes, but we need more detailed metrics to understand why the benchmark is so fast/slow. For example, with modern cache hierarchies, the cache-miss rate is a major indicator of performance. But also, due to super-scalar CPUs, the number of finished instructions per cycle can be equally as important because it indicates how good we are at utilizing the CPU's resources.

Often, we measure those metrics with a separate benchmark program: the perf(1) tool. If you haven't heard of perf, you should immediately stop reading and look at the perf man page as well as Brendan Gregg's resources on flame graphs.

Are you back again? Ok, good... But sometimes, we cannot easily rip out the function-under-test from a larger program but we have to measure it in its environment, which also fosters the realism of the results. With perf_event_open(2), which is also the basis for the perf tool, we're able to dynamically setup performance monitoring in our programs.

With the perf subsystem, Linux provides us with a powerful performance measurement infrastructure, which we can use to record all kinds of metrics during our benchmark execution. This does not only include hardware counters (e.g. executed instructions, cycles, cache misses), but also software metrics, like CPU migrations and page faults.

   int perf_event_open(struct perf_event_attr *attr,
               pid_t pid, int cpu, int group_fd, unsigned long flags);

In a nutshell, for every metric that you want to measure, you'd need to create a new perf probe with perf_event_open(), which returns a file descriptor to the probe. With attr you configure the probe and with (pid, cpu) you describe its measurement scope (this thread vs. the whole system). By passing an already created probe as group_fd, you can group multiple probes into a single multi-metric probe. After creation, you have to use the generic ioctl(2) system call to reset/enable/disable the probe:

ioctl(probe_fd, PERF_EVENT_IOC_ENABLE, 0);

To access the measured data, you just read(2) the data from the probe descriptor and the kernel returns the metrics according to attr.read_format.

Task

In the template, you'll find two algorithms that perform matrix multiplication. It is your task to use perf_event_open(2) to compare both algorithms. For this, you should measure the number of executed instructions, the passed cycles and the number of cache misses. With a reasonable sized matrix (2048x2048), I see the following numbers on my machine:

$ ./perf 2048
matrix_size: 32.00 MiB
drepper    19462.57M instr,     0.92 instr-per-cycle,     0.20 miss-per-instr
naive      77355.59M instr,     0.17 instr-per-cycle,     0.53 miss-per-instr

The "drepper" algorithm is the cache-optimized variant from "What Every Programmer Should Know About Memory" by Ulrich Drepper. From my results, we see that Drepper's variant requires less instructions (lower is better), has a higher instruction-per-cycle metric (higher is better), and a lower miss rate (lower is better).

Hints

With struct perf_handle and struct read_format, we already hinted one variant how you could design your APIs. In our implementation, a perf_handle is a multi-metric probe that contains enough information in the read_format to disseminate the read data.

Last modified: 2023-12-01 15:52:27.723613, Last author: , Permalink: /p/advent-19-perf


last changed 2023-12-01, 15:52 by Prof. Dr.-Ing. Christian Dietrich

For All Visitors

Vacancies of TU Braunschweig
Career Service' Job Exchange 
Merchandising

For Students

Term Dates
Courses
Degree Programmes
Information for Freshman
TUCard

Internal Tools

Glossary (GER-EN)
Change your Personal Data

Contact

Technische Universität Braunschweig
Universitätsplatz 2
38106 Braunschweig

P. O. Box: 38092 Braunschweig
GERMANY

Phone: +49 (0) 531 391-0

Getting here

© Technische Universität Braunschweig
Imprint Privacy Accessibility