Introduction aux Machines Virtuelles avec VMKit
Transcription
Introduction aux Machines Virtuelles avec VMKit
Introduction aux Machines Virtuelles avec VMKit VMKit un substrat de machine virtuelle Ingénieur : Harris Bakiras, Responsables : Gaël Thomas, Gilles Müller EQUIPE REGAL LIP6 – INRIA (Paris/France) Harris Bakiras Les environnements d'exécution Exécution code natif ( C, C++ ) VMKit: a substrate for Managed Runtime Environments 2 Les machines virtuelles Execution machine virtuelle (Java, C#) Code Codesource source Code Codesource source Machine Virtuelle vérificateur de byte-code Compilateur Compilateur Interpréteur Compilateur Compilateur Analyse Analyse Optimise Optimise Génère code binaire natif spécifique Génère code binaire natif spécifique Analyse Analyse Optimise Optimise Génère byte-code générique Génère byte-code générique Gestionnaire Threads Ramasse miette (GC) Byte-code Byte-codegénérique générique Machine MachineVirtuelle Virtuelle Analyse byte-code Analyse byte-code Évalue le byte-code ou Évalue le byte-code ou Compile et optimise en code binaire Compile et optimise en code binaire natif à la volée (JIT) natif à la volée (JIT) Harris Bakiras Allocateur mémoire Problèmes Code Codebinaire binairenatif natif execution execution Compilateur à la volée (JIT) VMKit: a substrate for Managed Runtime Environments execution execution 3 Développement extrêmement long à réaliser ! Comment tester une idée avec différents langages ? Comment implémenter une nouvelle VM efficace pour de nouveaux langages ? Comment étendre rapidement des langages existant ? Harris Bakiras VMKit: a substrate for Managed Runtime Environments 4 VMKit : un substrat de machine virtuelle But de VMKit : aider à expérimenter dans les VM VMKit : un substrat de machine virtuelle But de VMKit : aider à expérimenter dans les VM Objectif : factoriser les composants communs des VM Objectif : factoriser les composants communs des VM Compilateur Just In Time : génération de code natif à la volée Gestionnaire mémoire : alloue et collecte automatiquement la mémoire libre Gestionnaire de Thread : créé et synchronise les threads Personnalité Java (J3) Personnalité C# (N3 non maintenu) Personnalité R (Reactor) Personnalités Compilateur Just In Time : génération de code natif à la volée Gestionnaire mémoire : alloue et collecte automatiquement la mémoire libre Gestionnaire de Thread : créé et synchronise les threads Personnalité Java (J3) VMKit Compilateur Just In Time Harris Bakiras Gestionnaire mémoire Personnalité C# (N3 non maintenu) Personnalité R (Reactor) ~ 25k loc VMKit Gestionnaire de Thread Compilateur Just In Time VMKit: a substrate for Managed Runtime Environments 5 Harris Bakiras Gestionnaire mémoire Gestionnaire de Thread VMKit: a substrate for Managed Runtime Environments ~ 500k loc 6 VMKit : un substrat de machine virtuelle But de VMKit : aider à expérimenter dans les VM Objectif : factoriser les composants communs des VM Compilateur Just In Time : génération de code natif à la volée Gestionnaire mémoire : alloue et collecte automatiquement la mémoire libre Gestionnaire de Thread : créé et synchronise les threads VMKit d'un point de vue technique <<5% 5%du ducode code Personnalité Java (J3) Personnalité C# (N3 non maintenu) Personnalité R (Reactor) ~ 25k loc VMKit Compilateur Just In Time Harris Bakiras Gestionnaire mémoire Gestionnaire de Thread VMKit: a substrate for Managed Runtime Environments ~ 500k loc 7 Harris Bakiras VMKit: a substrate for Managed Runtime Environments 8 Implementation de VMKit Architecture détaillée Choix d'implémentation: repose sur des composants externes Personality Compilation related function GC related functions Execute Code Call back Compilateur à la volée (JIT) : LLVM [Lattner & Adve – CGO’04] Personality Method Gestionnaire mémoire : MMTk [Blackburn et Al. – ICSE’04] Entre JIT-C et gestionnaire mémoire = GC précis • Entre le gestionnaire de Thread et le gestionnaire mémoire = GC multi-threadé Invoke e Ge ner at MMTk G en er a te Clone Object gcmalloc/ write barrier Stack Maps Find thread Roots Posix Thread delegate Invoke VMKit = glue entre les différents composants Modify Trace Header Object LLVM Stub Gestionnaire de Thread : Posix Find global Roots Thread Management/ handshake Native Code VMKit Input/Output Operating System Harris Bakiras VMKit: a substrate for Managed Runtime Environments 9 Harris Bakiras VMKit: a substrate for Managed Runtime Environments 10 Architecture de la ToyVM Personality Skeleton VM Minimal (ToyVM) pour le tutoriel VMKit Materializer Object layout ToyCompiler ToyThread Execute Code ToyVM VMKit Operating System ToyRoot GVMaterializer (llvm) VirtualMachine GC MutatorThread VMKit Harris Bakiras VMKit: a substrate for Managed Runtime Environments 11 Harris Bakiras VMKit: a substrate for Managed Runtime Environments 12 Architecture de la ToyVM ToyVM et ToyThread ToyVM ← VirtualMachine Personality Skeleton Materializer Object layout ToyCompiler ToyThread Execute Code ToyVM VirtualMachine GC MutatorThread Traçage des variables globales Gestion des exceptions ToyThread ← MutatorThread ← Thread ToyRoot GVMaterializer (llvm) Gestion des Threads Point d'entrée du Garbage collector Backtrace (parcours de la pile d’exécution) Rendez-vous du garbage collector Scan de la pile d'exécution lors du GC Traçage des variables locales Méthode principale d'exécution VMKit Harris Bakiras VMKit: a substrate for Managed Runtime Environments 13 Harris Bakiras 14 Architecture de la ToyVM Architecture de la ToyVM Personality Skeleton Personality Skeleton ToyThread Execute Code ToyCompiler ToyVM Materializer GC ToyVM ToyRoot VirtualMachine GC MutatorThread VMKit VMKit: a substrate for Managed Runtime Environments Execute Code GVMaterializer (llvm) VirtualMachine MutatorThread ToyThread VMKit ToyRoot GVMaterializer (llvm) Object layout Materializer Object layout ToyCompiler Harris Bakiras VMKit: a substrate for Managed Runtime Environments VMKit 15 Harris Bakiras VMKit: a substrate for Managed Runtime Environments 16 ToyRoot (tag) ToyRoot (tag) ToyRoot ← vmkit::gc - Tagger les objets (stack maps) ToyRoot ← vmkit::gc - Tagger les objets (stack maps) [ ….... ] [ ….... ] ToyRoot* F (ToyRoot* param) { TOY_PARAM(param) ; TOY_VAR(ToyRoot, val) ; [ init val … ] val = g(param, val) ; return val ; } ToyRoot* F (ToyRoot* param) { TOY_PARAM(param) ; TOY_VAR(ToyRoot, val) ; [ init val … ] val = g(param, val) ; return val ; } ToyRoot* G (ToyRoot* a, ToyRoot* b){ TOY_PARAM(a) ; TOY_PARAM(b) ; TOY_VAR(ToyRoot, res) ; res = a.doSomething(b) ; return res ; } ToyRoot* G (ToyRoot* a, ToyRoot* b){ TOY_PARAM(a) ; TOY_PARAM(b) ; TOY_VAR(ToyRoot, res) ; res = a.doSomething(b) ; return res ; } [ ….... ] [ ….... ] Harris Bakiras VMKit: a substrate for Managed Runtime Environments 17 Harris Bakiras ToyRoot (tag) ToyRoot ← vmkit::gc - Tagger les objets (stack maps) Call stack ... param parameters ToyRoot* F (ToyRoot* param) { TOY_PARAM(param) ; TOY_VAR(ToyRoot, val) ; [ init val … ] val = g(param, val) ; return val ; } F 18 a b - Bits personnalisables parameters G Local vars res header VMKit: a substrate for Managed Runtime Environments - Bits de lock (J3) Local vars val Stack Maps ToyRoot ← vmkit::gc - Tagger les objets (stack maps) - header GC - Bits de marquage - Bits de hash [ ….... ] Harris Bakiras VMKit: a substrate for Managed Runtime Environments ToyRoot (header gc) [ ….... ] ToyRoot* G (ToyRoot* a, ToyRoot* b){ TOY_PARAM(a) ; TOY_PARAM(b) ; TOY_VAR(ToyRoot, res) ; res = a.doSomething(b) ; return res ; } Stack Maps 19 Harris Bakiras ToyRoot Object VMKit: a substrate for Managed Runtime Environments 20 ToyRoot (header gc) ToyRoot ← vmkit::gc - Tagger les objets (stack maps) - header GC - Bits de marquage - Bits de hash ToyRoot (tracer) ToyRoot ← vmkit::gc - Tagger les objets (stack maps) - header GC - Bits de marquage - Bits de hash - Bits de lock (J3) - Bits personnalisables - Méthode de Traçage de l'objet - Bits de lock (J3) - Bits personnalisables - Méthode de Traçage de l'objet Call stack rk ma ... destructor Op. delete tracer ToyRoot 1 markAndTrace mark ce tracer Tra And val ToyRoot Object ToyRoot 3 VMKit: a substrate for Managed Runtime Environments 21 Harris Bakiras ToyRoot (tracer) VMKit: a substrate for Managed Runtime Environments 22 Architecture de la ToyVM Personality Skeleton - Bits de lock (J3) ToyCompiler - Bits personnalisables - Méthode de Traçage de l'objet Materializer Object layout ToyRoot ← vmkit::gc - Tagger les objets (stack maps) - header GC - Bits de marquage - Bits de hash e rac ToyRoot 2 F tracer header VT C++ Harris Bakiras param Stack Maps dT An ToyThread Execute Code ToyVM VMKit - Allocateur GC - Surcharge operator new - Appel à new interdit pour gc (paramètre opaque) ToyRoot GVMaterializer (llvm) VirtualMachine GC MutatorThread VMKit Harris Bakiras VMKit: a substrate for Managed Runtime Environments 23 Harris Bakiras VMKit: a substrate for Managed Runtime Environments 24 Architecture de la ToyVM ToyCompiler (load IR) Personality Skeleton Object layout ToyCompiler ← GVMaterializer ToyCompiler ToyThread Récupération du code IR (représentation intermédiaire LLVM) .cc .cc ToyVM Execute Code Materializer .cc .cc .cc .cc ToyRoot GVMaterializer (llvm) VirtualMachine GC MutatorThread VMKit Harris Bakiras VMKit: a substrate for Managed Runtime Environments 25 Harris Bakiras VMKit: a substrate for Managed Runtime Environments ToyCompiler (load IR) ToyCompiler (load IR) ToyCompiler ← GVMaterializer ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) .cc .cc .cc .cc .bc .bc .bc .bc .cc .cc Récupération du code IR (représentation intermédiaire LLVM) .cc .cc .bc .bc VMKit .cc .cc .bc .bc .cc .cc .bc .bc .bc .bc toyVM.bc toyVM.bc Harris Bakiras 26 VMKit: a substrate for Managed Runtime Environments 27 Harris Bakiras opt toyVM.bc toyVM.bc VMKit: a substrate for Managed Runtime Environments 28 ToyCompiler (load IR) ToyCompiler (generate IR) ToyCompiler ← GVMaterializer ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) .cc .cc .cc .cc Récupération du code IR (représentation intermédiaire LLVM) JIT compiling toyVM-runtime.bc .cc .cc ToyCompiler ToyThread module .bc .bc .bc .bc IR IR .bc .bc Load at runtime toyVM.bc toyVM.bc IR IR toyVM toyVM execute(...) { … func = compiler.jitCompile(...); func(...); … } bin bin opt toyVM.bc toyVM.bc toyVM-content.bc Harris Bakiras VMKit: a substrate for Managed Runtime Environments 29 pass manager Harris Bakiras ToyCompiler (generate IR) ToyCompiler Récupération du code IR (représentation intermédiaire LLVM) JIT compiling ToyCompiler ToyThread module jitCompile module IR IR toyVM toyVM LLVM func No body IR IR toyVM toyVM execute(...) { … func = compiler.jitCompile(...); func(...); … } Harris Bakiras 30 ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) JIT compiling pass manager VMKit: a substrate for Managed Runtime Environments ToyCompiler (generate IR) ToyCompiler ← GVMaterializer execution engine Materialize execute(...) { … func = compiler.jitCompile(...); func(...); … } pass manager 31 LLVM func No body GenerateCode execution engine VMKit: a substrate for Managed Runtime Environments ToyThread jitCompile Harris Bakiras execution engine VMKit: a substrate for Managed Runtime Environments 32 ToyCompiler (generate IR) ToyCompiler (optimize IR) ToyCompiler ← GVMaterializer ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) JIT compiling ToyCompiler module IR IR toyVM toyVM Materialize Récupération du code IR (représentation intermédiaire LLVM) JIT compiling ToyCompiler ToyThread jitCompile module LLVM func No body IR IR toyVM toyVM execute(...) { … func = compiler.jitCompile(...); func(...); … } GenerateCode LLVM func No opt pass manager Harris Bakiras Harris Bakiras ToyCompiler IR IR toyVM toyVM pass manager VMKit: a substrate for Managed Runtime Environments 34 Récupération du code IR (représentation intermédiaire LLVM) JIT compiling ToyCompiler ToyThread LLVM func No body execute(...) { LLVM func opt Harris Bakiras jitCompile Materialize execution engine ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) JIT compiling module … func = compiler.jitCompile(...); func(...); … } ToyCompiler (optimize IR) ToyCompiler ← GVMaterializer execute(...) { pass manager 33 LLVM func No body LLVM func opt ToyCompiler (optimize IR) Materialize GenerateCode execution engine VMKit: a substrate for Managed Runtime Environments ToyThread jitCompile ToyThread module jitCompile IR IR toyVM toyVM LLVM func opt execute(...) { … func = compiler.jitCompile(...); func(...); … } … func = compiler.jitCompile(...); func(...); … } execution engine VMKit: a substrate for Managed Runtime Environments pass manager 35 Harris Bakiras execution engine VMKit: a substrate for Managed Runtime Environments 36 ToyCompiler (IR to native) ToyCompiler (GC Infos) ToyCompiler ← GVMaterializer ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) JIT compiling ToyCompiler module IR IR toyVM toyVM ToyCompiler jitCompile jitCompile LLVM func opt IR IR toyVM toyVM LLVM func opt Native func opt execution engine pass manager VMKit: a substrate for Managed Runtime Environments 37 Harris Bakiras ToyCompiler ToyThread module jitCompile IR IR toyVM toyVM LLVM func opt execute(...) { Native func opt Harris Bakiras … func = compiler.jitCompile(...); func(...); … } execution engine VMKit: a substrate for Managed Runtime Environments 38 ToyCompiler ← GVMaterializer Récupération du code IR (représentation intermédiaire LLVM) JIT compiling pass manager ToyThread ToyCompiler ToyCompiler ← GVMaterializer ToyVM execute(...) { … func = compiler.jitCompile(...); func(...); … } ToyCompiler (IR to native) func Stack Maps module execute(...) { pass manager Récupération du code IR (représentation intermédiaire LLVM) JIT compiling ToyThread Native func opt Harris Bakiras … func = compiler.jitCompile(...); func(...); … } Récupération du code IR (représentation intermédiaire LLVM) JIT compiling Generation d'IR Optimisation de l'IR Conversion de l'IR vers code natif Transmission des StackMaps (information variables locales) au GC execution engine VMKit: a substrate for Managed Runtime Environments 39 Harris Bakiras VMKit: a substrate for Managed Runtime Environments 40 A vos claviers ! lib/vmkit/MMTk/ include/vmkit include/llvm MutatorThread.h LLVM VMKit Allocator.h Thread.h Questions ? GVMaterializer.h VirtualMachine.h VmkitGC.h /lib TOYVM Harris Bakiras VMKit: a substrate for Managed Runtime Environments 41 ToyThread (.h / .cc) Harris Bakiras ToyVM (.h / .cc) ToyRoot (.h / .cc) Tracers.cc ToyCompiler (.h / .cc) util.h VMKit: a substrate for Managed Runtime Environments 42