Technische Universität Braunschweig
  • Studium & Lehre
    • Vor dem Studium
      • Informationen für Studieninteressierte
      • Studiengänge von A-Z
      • Bewerbung
      • Fit4TU - Self-Assessment
      • Beratungsangebote für Studieninteressierte
      • Warum Braunschweig?
    • Im Studium
      • Erstsemester-Hub
      • Semestertermine
      • Lehrveranstaltungen
      • Studien-ABC
      • Studienorganisation
      • Beratungsnavi
      • Zusatzqualifikationen
      • Finanzierung und Kosten
      • Besondere Studienbedingungen
      • Gesundheit & Wohlbefinden
      • Campusleben
    • Nach dem Studium
      • Exmatrikulation und Vorlegalisation
      • Nach dem Abschluss
      • Alumni*ae
    • Strategien und Qualitätsmanagement
      • Strategiepapiere für Studium und Lehre
      • Studienqualitätsmittel
      • Studiengangsentwicklung
      • Qualitätsmanagement
      • Systemakkreditierung
      • Rechtliche Grundlagen
      • TU Lehrpreis
    • Für Lehrende
      • Informationen für Lehrende
      • Konzepte
      • Lernmanagementsystem Stud.IP
    • Kontakt
      • Studienservice-Center
      • Sag's uns - in Studium und Lehre
      • Zentrale Studienberatung
      • Immatrikulationsamt
      • Abteilung 16 - Studium und Lehre
      • Career Service
      • Projekthaus
  • Forschung
    • Forschungsprofil
      • Forschungsschwerpunkte
      • Exzellenzcluster der TU Braunschweig
      • Forschungsprojekte
      • Forschungszentren
      • Forschungsprofile der Professuren
    • Frühe Karrierephase
      • Förderung in den frühen Phasen der wissenschaftlichen Karriere
      • Promotion
      • Postdocs
      • Nachwuchsgruppenleitung
      • Junior Professur und Tenure-Track
      • Habilitation
      • Service-Angebote für Wissenschaftler*innen
    • Forschungsdaten & Transparenz
      • Transparenz in der Forschung
      • Forschungsdaten
      • Open Access Strategie
      • Digitale Forschungsanzeige
    • Forschungsförderung
      • Netzwerk Forschungsförderung
      • Datenbanken und Stiftungen
    • Kontakt
      • Forschungsservice
      • Graduiertenakademie
  • International
    • Internationale Studierende
      • Warum Braunschweig?
      • Studium mit Abschluss
      • Austauschstudium
      • TU Braunschweig Summer School
      • Geflüchtete
      • International Student Support
    • Wege ins Ausland
      • Studium im Ausland
      • Praktikum im Ausland
      • Lehren und Forschen im Ausland
      • Arbeiten im Ausland
    • Internationale Forschende
      • Welcome Support
      • Promotionsstudium
      • Service für gastgebende Einrichtungen
    • Sprachen und interkulturelle Kompetenzvermittlung
      • Deutsch lernen
      • Fremdsprachen lernen
      • Interkulturelle Kompetenzvermittlung
    • Internationales Profil
      • Internationalisierung
      • Internationale Kooperationen
      • Strategische Partnerschaften
      • Internationale Netzwerke
    • International House
      • Wir über uns
      • Kontakt & Sprechstunden
      • Aktuelles und Termine
      • International Days
      • 5. Studentische Konferenz: Internationalisierung der Hochschulen
      • Newsletter, Podcast & Videos
      • Stellenausschreibungen
  • Die TU Braunschweig
    • Unser Profil
      • Ziele & Werte
      • Ordnungen und Leitlinien
      • Allianzen & Partner
      • Hochschulentwicklung 2030
      • Stiftungsuniversität
      • Internationale Strategie
      • Fakten & Zahlen
      • Unsere Geschichte
    • Karriere
      • Arbeiten an der TU
      • Stellenmarkt
      • Berufsausbildung an der TU
    • Wirtschaft & Unternehmen
      • Unternehmensgründung
      • Freunde & Förderer
    • Öffentlichkeit
      • Veranstaltungskalender
      • Check-in für Schüler*innen
      • Hochschulinformationstag (HIT)
      • Kinder-Uni
      • Das Studierendenhaus
      • Gasthörer*innen & Senior*innenstudium
      • Nutzung der Universitätsbibliothek
    • Presse & Kommunikation
      • Stabsstelle Presse und Kommunikation
      • Medienservice
      • Ansprechpartner*innen
      • Tipps für Wissenschaftler*innen
      • Themen und Stories
    • Kontakt
      • Allgemeiner Kontakt
      • Anreise
      • Für Hinweisgeber
  • Struktur
    • Leitung & Verwaltung
      • Das Präsidium
      • Stabsstellen
      • Verwaltung
      • Organe, Statusgruppen und Kommissionen
    • Fakultäten
      • Carl-Friedrich-Gauß-Fakultät
      • Fakultät für Lebenswissenschaften
      • Fakultät Architektur, Bauingenieurwesen und Umweltwissenschaften
      • Fakultät für Maschinenbau
      • Fakultät für Elektrotechnik, Informationstechnik, Physik
      • Fakultät für Geistes- und Erziehungswissenschaften
    • Institute
      • Institute von A-Z
    • Einrichtungen
      • Universitätsbibliothek
      • Gauß-IT-Zentrum
      • Zentrale Personalentwicklung
      • International House
      • Projekthaus
      • Transferservice
      • Hochschulsportzentrum
      • Einrichtungen von A-Z
    • Studierendenschaft
      • Studierendenparlament
      • Fachschaften
      • Studentische Wahlen
    • Lehrer*innenbildung
      • Lehrer*innenfortbildung
      • Forschung
    • Chancengleichheit
      • Gleichstellung
      • Familie
      • Diversität
    • Kontakt
      • Personensuche
  • Suche
  • Schnellzugriff
    • Personensuche
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Mensa
    • TUconnect (Studierendenportal)
    • Lehrveranstaltungen
    • Im Notfall
    • Stud.IP
    • UB Katalog
    • Status GITZ-Dienste
    • Störungsmeldung GB3
    • IT Dienste
    • Informationsportal (Beschäftigte)
    • Beratungsnavi
    • Linksammlung
    • DE
    • EN
    • IBR YouTube
    • Facebook
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
Menü
  • Struktur
  • Fakultäten
  • Carl-Friedrich-Gauß-Fakultät
  • Institute
  • Institut für Betriebssysteme und Rechnerverbund
  • Prof. Dr.-Ing. Christian Dietrich
  • Advent(2)
  • Send a Letter to Santa. Fast.
Logo IBR
IBR Login
  • Institut für Betriebssysteme und Rechnerverbund
    • News
    • Wir über uns
      • Gesamtes Team
      • Anreise
      • Raumplan
      • Projekte
      • Veröffentlichungen
      • Software
      • News Archiv
    • Connected and Mobile Systems
      • Team
      • Lehrveranstaltungen
      • Abschlussarbeiten
      • Projekte
      • Veröffentlichungen
      • Software
      • Datensätze
    • Verlässliche Systemsoftware
      • Übersicht
      • Team
      • Lehre
      • Arbeiten & Jobs
      • Forschung
      • Publikationen
    • Algorithmik
      • Team
      • Lehrveranstaltungen
      • Abschlussarbeiten
      • Projekte
      • Veröffentlichungen
    • Mikroprozessorlabor
    • Studium
      • Sommersemester 2025
      • Wintersemester 2024/2025
      • Abschlussarbeiten
    • Service
      • Bibliothek
      • Mailinglisten
      • Webmail
      • Knowledgebase
      • Wiki
      • Account Management
      • Service-Status
    • Spin-Offs
      • Docoloc
      • bliq (formerly AIPARK)
      • Confidential Technologies
    • Forschungsverbünde
      • IST.hub
  • Task Overview
  • Git repository
  • Mailing list
  • Matrix-Channel

Send a Letter to Santa. Fast.

☃️
Git-Repository: Template Solution Solution-Diff (Solution is posted at 18:00 CET)
Workload: 44 lines of code
Important System-Calls: sendfile(2), memfd_create(2)
Recommended Reads:
  • Dec. 1: The cat on the tip of the iceberg file 45 lines [open(2), pread(2), close(2)]
  • Dec. 10: Pipes Full of Gravy ipc 131 lines [epoll_create(2), epoll_ctl(2), epoll_wait(2), pipe(7), splice(2)]
Illustration for this exercsie

Children have a huge artistic potential and often express themselves graphically when preparing their wishlist. The ELFs know that and have adjusted their digital letter processing pipeline for those colorful letters: The incoming letters are scanned, saved as JPEGs, and the original paper is used for firing the cookie ovens. The digital letter is then OCR'ed and a neural network identifies and normalizes the drawings into simplified vector graphics, which are fed together with the extracted wishlist text into a gift-classifier network that produces a vector of possible gifts for each child. In batches of a few thousand letters, the pipeline creates an ILP problem to optimize the happiness of all children under the limited time budget. After all ELFs cannot invoke magic, but they have but a million tiny hands to work to craft teddies, ponies, and toy tanks.

Although this pipeline already sounds quite sophisticated, the ELFs identified some bottlenecks that are related to copying the large scanned letters around. To investigate on this, it is your task to find the fastest way to copy a file around.

sendfile

Yesterday, we have learned about splice(2), which we can use to splice data from or into a pipe without copying the data to the user space first. But what happens, if we would invoke splice with two real open files? The man page's ERRORS section is quite disappointing about this use-case which would be useful for our task:

   EINVAL Neither of the file descriptors refers to a pipe.

But, Linux has not only one system call to copy data between file descriptors, but multiple ones that can be used in different use cases:

  • splice(2) -- move data if source or destination is a pipe
  • tee(2) -- like splice but does not consume data at the source, but copy it.
  • vmsplice(2) -- a specialized writev() function that splices an scattered buffer into a pipe.
  • sendfile(2) -- move data between arbitrary file descriptors.
  • copy_file_range(2) -- Like splice() but both files have to be files on the same file system.

While I do not understand why Linux requires 5(!) different system calls to move/copy data between file descriptors, sendfile() is the one with the least constraints in usage. Therefore, we want to look at it more closely in today's task and measure how much faster we can do a file copy with sendfile.

memfd_create

What is a file? Is a file those "things" on the hard disk or SSD that continue to exist if I turn off the computer? Or are files those things that I can invoke open() on? What is the essence of a file; that essence that remains if we strip away everything that we do not consider essential.

Let's start then: (1) With a tmpfs(5), we can mount a file system whose data is located only in volatile RAM; so not every file is persistent. (2) On Unix, if you open a file and unlink(2) the name from the file system, the file can no longer be opened, but you can use the descriptor as long as you don't close it; so having a name is also not essential for a file. What is essential, is that we can read from a file, write to a file, and seek around in its open file descriptors. One could say that a file is a linear stream of bytes that is addressed at the system call interface with a file descriptor.

And sometimes this is exactly what you want: an anonymous, in-RAM file that you have a descriptor for. And for exactly this use case, Linux 3.17 learned the memfd_create(2) system call which can be used, without a mounted tmpfs, to create a new anonymous file in memory. With that you could, for example, simulate a std::stringstream in C:

int fd = memfd_create("name-for-debugging", 0);
FILE *f = fdopen(fd, "rw");
int bytes = fprintf(f, "Hello to %d ELFs!", elf_count);
pread(fd, buf, bytes, 0)
fclose(f);

Task

Today's task is to write a benchmark program that compares the performance of sendfile() against a regular read()/write() loop. To make the results comparable, we use a memfd file as target for the copy operation.

In the template code, we already give you a benchmarking harness that invokes memfd_create for you. If you think this is too boring, you can just skip the template and start with an empty file for this exercise. Really, today is all about observing the effects of sendfile().

As reference, I observed the following numbers on my machine:

$ ROUNDS=100 ./sendfile test.jpg
....
[read/write] copied with 1456.69 MiB/s (in 0.01 s, 139 syscalls)
[  sendfile] copied with 2213.98 MiB/s (in 0.00 s, 2 syscalls)
[read/write] copied with 1889.88 MiB/s (in 0.00 s, 139 syscalls)
[  sendfile] copied with 2013.52 MiB/s (in 0.00 s, 2 syscalls)
[read/write] copied with 2095.71 MiB/s (in 0.00 s, 139 syscalls)
[  sendfile] copied with 2278.85 MiB/s (in 0.00 s, 2 syscalls)
[read/write] copied with 1489.59 MiB/s (in 0.01 s, 139 syscalls)
[  sendfile] copied with 2102.52 MiB/s (in 0.00 s, 2 syscalls)
[read/write] copied with 1927.53 MiB/s (in 0.00 s, 139 syscalls)
[  sendfile] copied with 2020.50 MiB/s (in 0.00 s, 2 syscalls)
[read/write] copied with 1940.49 MiB/s (in 0.00 s, 139 syscalls)
[  sendfile] copied with 2275.93 MiB/s (in 0.00 s, 2 syscalls)
[read/write] copied with 1445.30 MiB/s (in 0.01 s, 139 syscalls)
sendfile: 1705.70 MiB/s, read/write: 1560.81 MiB/s

Hints

  • write() and sendfile() can return short. Therefore, you have to loop them until all bytes are written.
  • For read/write, you should play a little bit with the buffer size. As a reference, GNU cp uses an 128KiB buffer to copy data between file descriptors. What do you observe with smaller buffers? What do you observe with larger buffers?
  • Count the number of system calls by just placing increment operations at neuralgic points (read/write/sendfile) to get an idea about the complexity of the different strategies.

Last modified: 2023-12-01 15:52:27.890636, Last author: , Permalink: /p/advent-11-sendfile


aktualisiert am 01.12.2023, 15:52 von Prof. Dr.-Ing. Christian Dietrich

Für alle

Stellen der TU Braunschweig
Jobbörse des Career Service
Merchandising
Sponsoring- & Spendenleistungen
Drittmittelgeförderte Forschungsprojekte
Vertrauenspersonen für Hinweisgeber

Für Studierende

Semestertermine
Lehrveranstaltungen
Studiengänge von A-Z
Informationen für Erstsemester
TUCard

Interne Tools

Status GITZ-Dienste
Handbuch für TYPO3 (Intern)
Corporate Design-Toolbox (Intern)
Glossar (DE-EN)
Meine Daten ändern
Hochschulöffentliche Bekanntmachungen

Kontakt

Technische Universität Braunschweig
Universitätsplatz 2
38106 Braunschweig
Postfach: 38092 Braunschweig
Telefon: +49 (0) 531 391-0

Anreise

© Technische Universität Braunschweig
Impressum Datenschutz Barrierefreiheit