CERN IT Department CH-1211 Genève 23 Switzerland www.cern.ch/i t Multithreading in CASTOR How to use pthreads without seeing them (almost…) Giuseppe Lo.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Technical Design Technology choices Core framework Castor Readiness Review – June 2006 Giuseppe Lo Presti, German Cancio, Sebastien Ponce CERN / IT.
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.
3.5 Interprocess Communication
Client Server Design Alternatives© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
 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.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Process Concept An operating system executes a variety of programs
© 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.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Introduction to Processes CS Intoduction to Operating Systems.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
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 t Multithreading in CASTOR Experiences from a real life application Giuseppe Lo Presti (IT/DM)
CS333 Intro to Operating Systems Jonathan Walpole.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
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 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.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
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.
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.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Chapter 4 – Thread Concepts
Chapter 4: Threads.
Operating Systems Review ENCE 360.
Process Management Process Concept Why only the global variables?
Concurrency, Processes and Threads
Chapter 4 – Thread Concepts
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Operating System (013022) Dr. H. Iwidat
Linux 202 Training Module Program and Process.
Chapter 3: Process Concept
Chapter 4: Threads.
Threads and Data Sharing
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Mid Term review CSC345.
Jonathan Walpole Computer Science Portland State University
Concurrency, Processes and Threads
Chapter 4: Threads & Concurrency
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Concurrency, Processes and Threads
Presentation transcript:

CERN IT Department CH-1211 Genève 23 Switzerland t Multithreading in CASTOR How to use pthreads without seeing them (almost…) Giuseppe Lo Presti DM technical meeting – July 1 st, 2008

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 2 Outline Overview –Architecture and requirements A C++ framework for multithreading –Design 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 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 - 4 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 - 5 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 - 6 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 - 7 Simplified Class Diagram Programmer’s interface

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 8 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, supporting pipes for IPC 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 - 9 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 - 10 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 - 11 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 - 12 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 - 13 The Internals ForkedProcessPool encapsulates fork() calls, with children dispatch handled via select() void castor::server::ForkedProcessPool::init() throw (...) { // create pool of forked processes // we do it here so it is done before daemonization m_childPid = new int[m_nbThreads]; for (int i = 0; i < m_nbThreads; i++) {... castor::io::PipeSocket* ps = new castor::io::PipeSocket(); m_childPid[i] = 0; int pid = fork(); if(pid < 0) {... // error } else if(pid == 0) { // child... childRun(ps); // this is a blocking call to the user code exit(EXIT_SUCCESS); } else { // parent: save pipe and pid m_childPid[i] = pid; m_childPipe.push_back(ps); ps->closeRead(); int fd = ps->getFdWrite(); FD_SET(fd, &m_writePipes); // prepare mask for select() }

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 14 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 a customized system 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. Forked children are told to stop via SIGTERM too

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 15 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 – PipeSocket wraps a Unix pipe and allows object streaming between different processes (e.g. parent and children)

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 16 The Internals: full Class Diagram

CERN IT Department CH-1211 Genève 23 Switzerland t Internet Services Giuseppe Lo Presti, Multithreading in CASTOR - 17 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 - 18 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 - 19 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… The 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?