changeset 324:cecaa790a23e draft

Merge remote branch 'refs/remotes/svn/trunk' into svn
author Gavin Andresen <gavinandresen@gmail.com>
date Thu, 09 Sep 2010 14:26:53 -0400
parents bf9b56c332e4 (current diff) ae8c8d8f84bf (diff)
children 0f88bd6af0eb
files cryptopp/secblock.h headers.h init.cpp main.cpp net.cpp serialize.h util.cpp util.h
diffstat 8 files changed, 89 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cryptopp/secblock.h
+++ b/cryptopp/secblock.h
@@ -184,8 +184,8 @@
 
 	void deallocate(void *p, size_type n)
 	{
-        //// Bitcoin: can't figure out why this is tripping on a few compiles.
-        //assert(false);
+		//// Bitcoin: don't know why this trips, probably a false alarm, depends on the compiler used. 
+		//assert(false);
 	}
 
 	size_type max_size() const {return 0;}
--- a/headers.h
+++ b/headers.h
@@ -99,6 +99,7 @@
 #include <net/if.h>
 #include <ifaddrs.h>
 #include <fcntl.h>
+#include <signal.h>
 #endif
 #ifdef BSD
 #include <netinet/in.h>
--- a/init.cpp
+++ b/init.cpp
@@ -10,7 +10,6 @@
 
 
 
-
 //////////////////////////////////////////////////////////////////////////////
 //
 // Shutdown
@@ -57,6 +56,11 @@
     }
 }
 
+void HandleSIGTERM(int)
+{
+    fRequestShutdown = true;
+}
+
 
 
 
@@ -130,6 +134,14 @@
 #ifndef __WXMSW__
     umask(077);
 #endif
+#ifndef __WXMSW__
+    // Clean shutdown on SIGTERM
+    struct sigaction sa;
+    sa.sa_handler = HandleSIGTERM;
+    sigemptyset(&sa.sa_mask);
+    sa.sa_flags = 0;
+    sigaction(SIGTERM, &sa, NULL);
+#endif
 
     //
     // Parameters
--- a/main.cpp
+++ b/main.cpp
@@ -2767,6 +2767,68 @@
 static const int NPAR = 32;
 extern void Double_BlockSHA256(const void* pin, void* pout, const void* pinit, unsigned int hash[8][NPAR], const void* init2);
 
+#ifdef __GNUC__
+void CallCPUID(int in, int& aret, int& cret)
+{
+    int a, c;
+    asm (
+        "mov %2, %%eax; " // in into eax
+        "cpuid;"
+        "mov %%eax, %0;" // eax into ret
+        "mov %%ecx, %1;" // eax into ret
+        :"=r"(a),"=r"(c) /* output */
+        :"r"(in) /* input */
+        :"%eax","%ecx" /* clobbered register */
+    );
+    aret = a;
+    cret = c;
+}
+
+bool Detect128BitSSE2()
+{
+    int a, c, nBrand;
+    CallCPUID(0, a, nBrand);
+    bool fIntel = (nBrand == 0x6c65746e); // ntel
+    bool fAMD = (nBrand == 0x444d4163); // cAMD
+
+    struct
+    {
+        unsigned int nStepping : 4;
+        unsigned int nModel : 4;
+        unsigned int nFamily : 4;
+        unsigned int nProcessorType : 2;
+        unsigned int nUnused : 2;
+        unsigned int nExtendedModel : 4;
+        unsigned int nExtendedFamily : 8;
+    }
+    cpu;
+    CallCPUID(1, a, c);
+    memcpy(&cpu, &a, sizeof(cpu));
+    int nFamily = cpu.nExtendedFamily + cpu.nFamily;
+    int nModel = cpu.nExtendedModel*16 + cpu.nModel;
+
+    // We need Intel Nehalem or AMD K10 or better for 128bit SSE2
+    // Nehalem = i3/i5/i7 and some Xeon
+    // K10 = Opterons with 4 or more cores, Phenom, Phenom II, Athlon II
+    //  Intel Core i5  family 6, model 26 or 30
+    //  Intel Core i7  family 6, model 26 or 30
+    //  Intel Core i3  family 6, model 37
+    //  AMD Phenom    family 16, model 10
+    bool fUseSSE2 = ((fIntel && nFamily * 10000 + nModel >=  60026) ||
+                     (fAMD   && nFamily * 10000 + nModel >= 160010));
+
+    static bool fPrinted;
+    if (!fPrinted)
+    {
+        fPrinted = true;
+        printf("CPUID %08x family %d, model %d, stepping %d, fUseSSE2=%d\n", nBrand, nFamily, nModel, cpu.nStepping, fUseSSE2);
+    }
+    return fUseSSE2;
+}
+#else
+bool Detect128BitSSE2() { return false; }
+#endif
+
 
 
 
@@ -2774,6 +2836,9 @@
 {
     printf("BitcoinMiner started\n");
     SetThreadPriority(THREAD_PRIORITY_LOWEST);
+    bool f4WaySSE2 = Detect128BitSSE2();
+    if (mapArgs.count("-4way"))
+        f4WaySSE2 = (mapArgs["-4way"] != "0");
 
     CKey key;
     key.MakeNewKey();
@@ -2913,7 +2978,6 @@
         //
         // Search
         //
-        bool f4WaySSE2 = mapArgs.count("-4way");
         int64 nStart = GetTime();
         uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
         uint256 hashbuf[2];
--- a/net.cpp
+++ b/net.cpp
@@ -1160,9 +1160,13 @@
                 pnode->Release();
         }
 
-        // Wait and allow messages to bunch up
+        // Wait and allow messages to bunch up.
+        // Reduce vnThreadsRunning so StopNode has permission to exit while
+        // we're sleeping, but we must always check fShutdown after doing this.
         vnThreadsRunning[2]--;
         Sleep(100);
+        if (fRequestShutdown)
+            Shutdown(NULL);
         vnThreadsRunning[2]++;
         if (fShutdown)
             return;
--- a/serialize.h
+++ b/serialize.h
@@ -23,7 +23,7 @@
 static const unsigned int MAX_SIZE = 0x02000000;
 
 static const int VERSION = 312;
-static const char* pszSubVer = ".1";
+static const char* pszSubVer = ".2";
 
 
 
--- a/util.cpp
+++ b/util.cpp
@@ -11,6 +11,7 @@
 bool fPrintToConsole = false;
 bool fPrintToDebugger = false;
 char pszSetDataDir[MAX_PATH] = "";
+bool fRequestShutdown = false;
 bool fShutdown = false;
 bool fDaemon = false;
 bool fCommandLine = false;
--- a/util.h
+++ b/util.h
@@ -140,6 +140,7 @@
 extern bool fPrintToConsole;
 extern bool fPrintToDebugger;
 extern char pszSetDataDir[MAX_PATH];
+extern bool fRequestShutdown;
 extern bool fShutdown;
 extern bool fDaemon;
 extern bool fCommandLine;