Changeset 888

Show
Ignore:
Timestamp:
04/05/08 19:28:16 (5 months ago)
Author:
Gregor
Message:

rebuild/Makefile, rebuild/mars.{c,h}, rebuild/nprocs.{c,h}: Added support for multi-process builds, including automatic detection of number of CPUs.

rebuild/mem.c: Now uses libgc.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rebuild/Makefile

    r887 r888  
    55 
    66GC=-Wl,-Bstatic,-lgc,-Bdynamic 
     7THREADS=-pthread 
    78 
    89EXEEXT= 
    910 
    1011LDFLAGS= 
    11 LIBS=$(GC) 
     12LIBS=$(GC) $(THREADS) 
    1213 
    1314PREFIX=/usr 
     
    5960  module.o \ 
    6061  mtype.o \ 
     62  nprocs.o \ 
    6163  opover.o \ 
    6264  optimize.o \ 
     
    7981 
    8082rebuild$(EXEEXT): id.c id.h impcnvtab.c $(OBJS) 
    81     $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LIBS) $(OBJS) -o rebuild$(EXEEXT) 
     83    $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o rebuild$(EXEEXT) 
    8284 
    8385id.o: id.c 
  • trunk/rebuild/deps

    r887 r888  
    119119  id.h enum.h import.h aggregate.h hdrgen.h 
    120120choosedc.o: choosedc.c whereami.h 
     121nprocs.o: nprocs.c 
    121122whereami.o: whereami.c 
    122123opover.o: opover.c mem.h mtype.h root.h dchar.h gnuc.h stringtable.h \ 
  • trunk/rebuild/mars.c

    r884 r888  
    2121#include <sys/types.h> 
    2222 
     23#include <pthread.h> 
     24#include "gc/gc.h" 
     25 
    2326#if _WIN32 
    2427#include <windows.h> 
     
    4144#include "module.h" 
    4245#include "mtype.h" 
     46#include "nprocs.h" 
    4347#include "response.h" 
    4448#include "id.h" 
     
    9599    this->filename = mod ? mod->srcfile->toChars() : NULL; 
    96100} 
     101 
     102/* 
     103 * Support for grouped compilation 
     104 */ 
     105class GroupedCompile { 
     106    public: 
     107    Array imodules, ofiles; 
     108    Array origonames, newonames; 
     109}; 
     110Array GroupedCompiles; 
     111unsigned int curcompile = 0; 
     112pthread_mutex_t compilemutex; 
     113void *compileThread(void *); 
     114 
    97115 
    98116/************************************** 
     
    250268    int argcstart = argc; 
    251269 
     270    mem.init(); 
     271 
    252272    // Check for malformed input 
    253273    if (argc < 1 || !argv) 
     
    281301    global.params.clean = 0; 
    282302    global.params.oneatatime = 0; 
     303    global.params.procs = 0; 
    283304    global.params.reflect = 0; 
    284305    global.params.candydoc = 0; 
     
    900921                global.params.candydoc = 1; 
    901922            } 
     923            else if (p[1] == 'j') 
     924            { 
     925                global.params.procs = atoi(p + 2); 
     926            } 
    902927            else if (strncmp(p + 1, "dc=", 3) == 0) {} 
    903928            else if (strncmp(p + 1, "CFPATH", 6) == 0 || 
     
    914939                compileFlags += " "; 
    915940                compileFlags += p; 
     941                linkFlags += " "; 
     942                linkFlags += p; 
     943                shliblinkFlags += " "; 
     944                shliblinkFlags += p; 
    916945                continue; 
    917946                 
     
    10231052        masterConfig[sect].find("oneatatime") != masterConfig[sect].end() && 
    10241053        masterConfig[sect]["oneatatime"] != "no") { 
     1054        global.params.oneatatime = 1; 
     1055    } 
     1056 
     1057    // and how many processes to use 
     1058    if (global.params.procs == 0) { 
     1059        global.params.procs = nprocs(); 
     1060        if (global.params.procs > 1) 
     1061            global.params.procs *= 2; 
     1062    } 
     1063    if (global.params.procs > 1) { 
    10251064        global.params.oneatatime = 1; 
    10261065    } 
     
    13001339    fatal();*/ 
    13011340     
    1302     class GroupedCompile { 
    1303         public: 
    1304         Array imodules, ofiles; 
    1305         Array origonames, newonames; 
    1306     }; 
    1307     Array GroupedCompiles; 
    13081341    GroupedCompiles.push((void *) new GroupedCompile); 
    13091342     
     
    16271660    mem.fullcollect(); 
    16281661     
    1629     // Now do the actual compilation 
    1630     for (unsigned int j = 0; global.params.obj && j < GroupedCompiles.dim; j++) { 
    1631         GroupedCompile *gc = (GroupedCompile *) GroupedCompiles.data[j]; 
    1632          
    1633         if (gc->imodules.dim == 0) continue; 
    1634          
    1635         // make a string of the file names 
    1636         std::string infiles; 
    1637         for (unsigned int k = 0; k < gc->imodules.dim; k++) { 
    1638             Module *m = (Module *) gc->imodules.data[k]; 
    1639             infiles += m->srcfile->name->str; 
    1640             infiles += " "; 
     1662    // Now do the actual compilation, across any number of processors 
     1663    if (global.params.procs > 1) { 
     1664        pthread_t threads[global.params.procs]; 
     1665        pthread_mutex_init(&compilemutex, NULL); 
     1666 
     1667        for (unsigned int j = 0; j < global.params.procs; j++) { 
     1668            pthread_create(&(threads[j]), NULL, compileThread, NULL); 
    16411669        } 
    1642          
    1643         // then compile 
    1644         runCompile(infiles); 
    1645          
    1646         // and rename 
    1647         for (unsigned int k = 0; k < gc->origonames.dim; k++) { 
    1648             if (global.params.listonly) { 
    1649                 printf("mv -f %s %s\n", 
    1650                        gc->origonames.data[k], gc->newonames.data[k]); 
    1651                  
    1652             } else { 
    1653                 if (access((char *) gc->origonames.data[k], F_OK) == 0) { 
    1654                     if (global.params.verbose) 
    1655                         printf("rename    %s to %s\n", 
    1656                                (char *) gc->origonames.data[k], 
    1657                                (char *) gc->newonames.data[k]); 
    1658                      
    1659                     remove((char *) gc->newonames.data[k]); // ignore errors 
    1660                     rename((char *) gc->origonames.data[k], 
    1661                            (char *) gc->newonames.data[k]); // ignore errors 
    1662                 } 
    1663             } 
     1670        for (unsigned int j = 0; j < global.params.procs; j++) { 
     1671            pthread_join(threads[j], NULL); 
    16641672        } 
     1673    } else { 
     1674        compileThread(NULL); 
    16651675    } 
    16661676     
     
    17111721 
    17121722    return status; 
     1723} 
     1724 
     1725/* 
     1726 * The thread in which actual compilation is performed 
     1727 */ 
     1728void *compileThread(void *ignore) 
     1729{ 
     1730    pthread_mutex_lock(&compilemutex); 
     1731    int tnum = rand(); 
     1732    while (curcompile < GroupedCompiles.dim) { 
     1733        GroupedCompile *gc = (GroupedCompile *) GroupedCompiles.data[curcompile]; 
     1734        curcompile++; 
     1735        pthread_mutex_unlock(&compilemutex); 
     1736 
     1737        if (gc->imodules.dim == 0) { 
     1738            pthread_mutex_lock(&compilemutex); 
     1739            continue; 
     1740        } 
     1741         
     1742        // make a string of the file names 
     1743        std::string infiles; 
     1744        for (unsigned int k = 0; k < gc->imodules.dim; k++) { 
     1745            Module *m = (Module *) gc->imodules.data[k]; 
     1746            infiles += m->srcfile->name->str; 
     1747            infiles += " "; 
     1748        } 
     1749         
     1750        // if we only have one file, just rename on the fly 
     1751        if (gc->imodules.dim == 1) { 
     1752            infiles += std::string("-of") + ((char *) gc->newonames.data[0]) + " "; 
     1753        } 
     1754         
     1755        // then compile 
     1756        runCompile(infiles); 
     1757         
     1758        // and rename 
     1759        if (gc->imodules.dim > 1) { 
     1760            for (unsigned int k = 0; k < gc->origonames.dim; k++) { 
     1761                if (global.params.listonly) { 
     1762                    printf("mv -f %s %s\n", 
     1763                           gc->origonames.data[k], gc->newonames.data[k]); 
     1764                 
     1765                } else { 
     1766                    if (access((char *) gc->origonames.data[k], F_OK) == 0) { 
     1767                        if (global.params.verbose) 
     1768                            printf("rename    %s to %s\n", 
     1769                               (char *) gc->origonames.data[k], 
     1770                               (char *) gc->newonames.data[k]); 
     1771                     
     1772                        remove((char *) gc->newonames.data[k]); // ignore errors 
     1773                        rename((char *) gc->origonames.data[k], 
     1774                               (char *) gc->newonames.data[k]); // ignore errors 
     1775                    } 
     1776                } 
     1777            } 
     1778        } 
     1779 
     1780        pthread_mutex_lock(&compilemutex); 
     1781    } 
     1782    pthread_mutex_unlock(&compilemutex); 
    17131783} 
    17141784 
  • trunk/rebuild/mars.h

    r840 r888  
    5454    char clean;         // clean after building 
    5555    char oneatatime;    // don't pass multiple files into the compiler 
     56    unsigned char procs;// number of processes to run concurrently 
    5657    char reflect;       // use drefgen 
    5758    char candydoc;      // generate modules.ddoc 
  • trunk/rebuild/mem.c

    r272 r888  
    11 
    22/* Copyright (c) 2000 Digital Mars  */ 
    3 /* All Rights Reserved             */ 
     3/* Copyright (c) 2008 Gregor Richards   */ 
    44 
    55#include <stdio.h> 
     
    77#include <string.h> 
    88 
     9#include "gc/gc.h" 
     10 
    911#include "mem.h" 
    10  
    11 /* This implementation of the storage allocator uses the standard C allocation package. 
    12  */ 
    1312 
    1413Mem mem; 
     
    1615void Mem::init() 
    1716{ 
     17    GC_INIT(); 
    1818} 
    1919 
     
    2424    if (s) 
    2525    { 
    26     p = ::strdup(s); 
     26    p = GC_strdup(s); 
    2727    if (p) 
    2828        return p; 
     
    3939    else 
    4040    { 
    41     p = ::malloc(size); 
     41    p = GC_malloc(size); 
    4242    if (!p) 
    4343        error(); 
     
    5353    else 
    5454    { 
    55     p = ::calloc(size, n); 
     55    //p = ::calloc(size, n); 
     56        p = GC_malloc(size*n); 
     57        memset(p, 0, size*n); 
    5658    if (!p) 
    5759        error(); 
     
    6466    if (!size) 
    6567    {   if (p) 
    66     {   ::free(p); 
     68    {   GC_free(p); 
    6769        p = NULL; 
    6870    } 
     
    7072    else if (!p) 
    7173    { 
    72     p = ::malloc(size); 
     74    p = GC_malloc(size); 
    7375    if (!p) 
    7476        error(); 
     
    7678    else 
    7779    { 
    78     p = ::realloc(p, size); 
     80    p = GC_realloc(p, size); 
    7981    if (!p) 
    8082        error(); 
     
    8688{ 
    8789    if (p) 
    88     ::free(p); 
     90    GC_free(p); 
    8991} 
    9092 
     
    9698    else 
    9799    { 
    98     p = ::malloc(size); 
     100    p = GC_malloc(size); 
    99101    if (!p) 
    100102        error(); 
     
    113115void Mem::fullcollect() 
    114116{ 
     117    GC_gcollect(); 
    115118} 
    116119 
     
    124127void * operator new(size_t m_size) 
    125128{    
    126     void *p = malloc(m_size); 
     129    void *p = GC_malloc(m_size); 
    127130    if (p) 
    128131    return p; 
     
    134137void operator delete(void *p) 
    135138{ 
    136     free(p); 
     139    GC_free(p); 
    137140} 
    138141