CERN IT Department CH-1211 Genève 23 Switzerland www.cern.ch/i t Multithreading in CASTOR Experiences from a real life application Giuseppe Lo Presti (IT/DM)

Slides:



Advertisements
Similar presentations
WHAT IS AN OPERATING SYSTEM? An interface between users and hardware - an environment "architecture ” Allows convenient usage; hides the tedious stuff.
Advertisements

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Technical Design Technology choices Core framework Castor Readiness Review – June 2006 Giuseppe Lo Presti, German Cancio, Sebastien Ponce CERN / IT.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Distributed systems Programming with threads. Reviews on OS concepts Each process occupies a single address space.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
Chapter 13 Embedded Systems
Figure 1.1 Interaction between applications and the operating system.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
CHEN Ge CSIS, HKU March 9, Jigsaw W3C’s Java Web Server.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
CERN - IT Department CH-1211 Genève 23 Switzerland Castor External Operation Face-to-Face Meeting, CNAF, October 29-31, 2007 CASTOR2 Disk.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
CASTOR evolution Presentation to HEPiX 2003, Vancouver 20/10/2003 Jean-Damien Durand, CERN-IT.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
CERN IT Department CH-1211 Genève 23 Switzerland t Multithreading in CASTOR How to use pthreads without seeing them (almost…) Giuseppe Lo.
Operating Systems Process Creation
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
CERN - IT Department CH-1211 Genève 23 Switzerland t High Availability Databases based on Oracle 10g RAC on Linux WLCG Tier2 Tutorials, CERN,
CERN IT Department CH-1211 Genève 23 Switzerland t HEPiX Conference, ASGC, Taiwan, Oct 20-24, 2008 The CASTOR SRM2 Interface Status and plans.
What is an Operating System? Various systems and their pros and cons –E.g. multi-tasking vs. Batch OS definitions –Resource allocator –Control program.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 12: I/O Systems I/O hardwared Application I/O Interface Kernel I/O.
Background Computer System Architectures Computer System Software.
Computing Facilities CERN IT Department CH-1211 Geneva 23 Switzerland t CF Cluman: Advanced Cluster Management for Large-scale Infrastructures.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4 – Thread Concepts
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Jean-Philippe Baud, IT-GD, CERN November 2007
Module 12: I/O Systems I/O hardware Application I/O Interface
Chapter 4: Threads.
Process Management Process Concept Why only the global variables?
Chapter 4 – Thread Concepts
CS399 New Beginnings Jonathan Walpole.
Operating System (013022) Dr. H. Iwidat
Linux 202 Training Module Program and Process.
Lecture 21 Concurrency Introduction
Distributed Systems - Comp 655
Operating Systems : Overview
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Operating System Concepts
Operating Systems : Overview
Mid Term review CSC345.
Operating Systems : Overview
Operating Systems Lecture 1.
February 5, 2004 Adrienne Noble
Chapter 4: Threads.
Module 12: I/O Systems I/O hardwared Application I/O Interface
Presentation transcript:

CERN IT Department CH-1211 Genève 23 Switzerland t Multithreading in CASTOR Experiences from a real life application Giuseppe Lo Presti (IT/DM)

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 2 Outline Castor: the CERN mass storage system –Facts & figures –Architectural overview A C++ framework for multithreading –Requirements and implementation –Some user code samples –The internals The framework in action Conclusions

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 3 Castor Overview Hierarchical Storage Manager –Disk cache + Tape archive Some facts & figures –In production for the Tier0 and a few Tier1s –CERN deployment: 650+ diskservers, 2.5+ PB of managed disk cache, 12+ PB on tape, several GB/s sustained I/O Disk cache network traffic during May CCRC:

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 4 Castor Architecture Overview Database centric –Stateless redundant software components –State stored in a central database for scalability and fault resiliency purposes Technology choices –A number of multithreaded daemons perform all needed tasks to serve user requests –Each operation is reflected in the database => tasks are inherently I/O bound or better “latency bound” Dominated by db/network latency –Concurrency issues resolved in the database by using locks

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 5 Outline Castor: the CERN mass storage system –Facts & figures –Architectural overview A C++ framework for multithreading –Requirements and implementation –Some user code samples –The internals The framework in action Conclusions

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 6 High Level Requirements Multithreading to achieve better overall throughput in terms of #requests/sec –System inherently superlinear because of I/O bound tasks Need for supporting thread pools –Each one dedicated to a different task Lightweight multithreading infrastructure –Limit memory footprint of the daemons Seamless integration with C++ Very limited issues with synchronization and data sharing across different threads –Context data is always in the db –Each thread deals with a different request: typical case of embarassing parallelism

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 7 A Framework for Multithreading Choices –Usage of Linux POSIX threads –C++ package to hide pthreads complexity and provide a Java-like interface IThread abstract class (cf. Java Runnable interface) Specialized thread pools to implement different functionalities (e.g. requests handling) Very high reusability across all software components –Ability to have thread-safe and thread-shared variables –Daemon mode with embedded signal handling Support for graceful stop and restart of daemons

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 8 Framework Implementation Usage of an existing OS abstraction layer: the Cthread API –Replicates all pthread API, and additionally provides thread-safe global variables –One of the most mature (read old…) parts in the Castor codebase, shared by different projects in IT C++ code –Clean interface for the user: generic methods to compose daemons out of user classes –Cthread / pthread / system calls are kept hidden from user code, but still accessible for special cases E.g. mutexes Typical use cases –Listening to a port and accepting requests –Polling the database for next operation to perform

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 9 Package Class Diagram Programmer’s interface

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 10 Main Classes Thread pools – ListenerThreadPool : generic socket connection dispatcher à-la Apache Specialized classes for TCP, UDP, … sockets – SignalThreadPool : pool manager for backend activities that need to run periodically or upon external signalling The signalling mechanism is based on condition variables – ForkedProcessPool : pool manager based on fork(), not on pthreads Classes for servers – BaseServer : basic generic server providing daemon mode (detach from shell) and logging initialization – BaseDaemon : more sophisticated base class for daemons, supporting system signal handling and any combinations of the implemented thread pools

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 11 Code Samples Excerpt from the Monitoring daemon’s main() –Different thread pools are mixed together –The start() method from BaseDaemon spawns all the requested threads RmMasterDaemon daemon;... // db threadpool daemon.addThreadPool(new castor::server::SignalThreadPool( "DatabaseActuator”, new DbActuatorThread( daemon.clusterStatus()), updateInterval)); daemon.getThreadPool('D')->setNbThreads(1); // update threadpool daemon.addThreadPool(new castor::server::UDPListenerThreadPool( "Update", new UpdateThread( daemon.clusterStatus()), listenPort));... // Start daemon daemon.parseCommandLine(argc, argv); daemon.start();

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 12 Code Samples User threads –As easy as inheriting from IThread: Typical pitfall: code is shared among all threads in each given pool –Mutex sections and synchronization to be explicitly implemented – no synchronized methods like in Java Consequence: class variables are thread-shared, only local variables are thread-safe –But you may need thread-safe singletons… Our solution (provided by Cthreads): for each thread-safe global variable, keep an hash map indexed by TID class UpdateThread : public castor::server::IThread { public: virtual void run(void *param) throw(); virtual void stop(); }

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 13 The Internals …So, where are the (p)threads? BaseThreadPool serves as basic infrastructure –A friend function _thread_run() is the thread entrypoint, which runs the user code –All specialized thread pools use this function when spawning threads void* castor::server::_thread_run(void* param) { struct threadArgs *args = (struct threadArgs*)param; castor::server::BaseThreadPool* pool = dynamic_cast (args->handler); // Executes the thread try { pool->m_thread->run(args->param); } catch(castor::exception::Exception any) { // error handling }

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 14 The Internals SignalThreadPool encapsulates pthread_create() calls and condition variables Threads wait until a condition variable gets notified, or after a timeout has passed – pthread_cond_wait() and pthread_cond_signal() –One (or more) thread in the pool is waken up and executes the user code –Pool keeps track of current # of busy threads void castor::server::SignalThreadPool::run() throw (...) {... // create pool of detached threads for (int i = 0; i < m_nbThreads; i++) { if (Cthread_create_detached( castor::server::_thread_run, args) >= 0) { ++n; // for later error handling }... }

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 15 The Internals BaseDaemon manages all threads and encapsulates the system signal handling –To avoid unpredictable behaviours, all threads need to be protected from signals via: pthread_sigmask(SIG_BLOCK, &signalSet, NULL) where signalSet includes all usual system signals –Yet another pthread performs the signal handling by looping on sigwait() –After spawning all user threads, the main thread waits for a notification from the dedicated signal handling thread, and broadcasts an appropriate message to all running threads E.g. on SIGTERM, all user threads’ stop() methods are called; after # of busy threads goes to 0, exit() is called.

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 16 The Internals Additional facilities in the framework – BaseDbThread implements the IThread interface and provides a graceful termination of a thread-specific database connection upon stop() – Mutex wraps common pthread functions to handle mutexes on integer variables wait() and signal() methods provided Generic mutexes on variables of any type left to the user code

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 17 Outline Castor: the CERN mass storage system –Facts & figures –Architectural overview A C++ framework for multithreading –Requirements and implementation –Some user code samples –The internals The framework in action Conclusions

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 18 The Framework in Action Class Diagram from Castor doxygen documentation –Most Castor daemons inherit from BaseDaemon –They all support graceful stop, e.g.: DATE= HOST=lxb1952.cern.ch LVL=System FACILITY=Stager PID=11439 […] MESSAGE="GRACEFUL STOP [SIGTERM] - Shutting down the service" DATE= HOST=lxb1952.cern.ch LVL=System FACILITY=Stager PID=11439 […] MESSAGE="GRACEFUL STOP [SIGTERM] - Shut down successfully completed"

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 19 The Framework in Action Typical load on a node –8 cores run a total of ~90 threads, each owning a db connection, with a fraction of the total available CPU and memory resources even during high load peaks The stager daemon alone runs 53 threads –This is the current deployment of a production Castor instance! top - 16:17:53 up 115 days, 11:00, 4 users, load average: 1.06, 0.78, 0.59 Tasks: 173 total, 2 running, 171 sleeping, 0 stopped, 0 zombie Cpu(s): 6.5% us, 1.9% sy, 0.0% ni, 91.3% id, 0.0% wa, 0.0% hi, 0.3% si Mem: k total, k used, k free, k buffers Swap: k total, 220k used, k free, k cached PID USER PR NI %CPU TIME+ %MEM VIRT RES SHR S COMMAND stage : m 32m 11m S migrator 3107 root : m 76m 5972 S dlfserver stage : m 32m 11m S migrator 3309 root : m 109m 9.8m S stagerDaemon 3315 root : m 109m 9.8m S stagerDaemon 3314 root : m 109m 9.8m S stagerDaemon 3238 root : m 29m 8380 S rhserver...

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 20 Conclusions We have shown how the pthread API can be powerful enough to support many high level multithreaded tasks –But don’t forget that we started with an embarassing parallelism scenario… CASTOR service moved from 6 dual CPU nodes to one 8-cores node –No way out of multithreading I know, that’s become pretty obvious by now… Comments, questions?