All pastes #165521 Raw Edit

fastHash

public text v1 · immutable
·published 2006-09-09 23:04 UTC
rendered paste body
? build
? linuxdcpp
Index: client/HashManager.cpp
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/client/HashManager.cpp,v
retrieving revision 1.8
diff -u -r1.8 HashManager.cpp
--- client/HashManager.cpp	23 Jun 2006 19:51:48 -0000	1.8
+++ client/HashManager.cpp	9 Sep 2006 22:58:50 -0000
@@ -27,6 +27,10 @@
 #include "ZUtils.h"
 #include "SFVReader.h"
 
+#ifndef _WIN32
+#include <sys/mman.h> // mmap, munmap, madvise
+#endif
+
 #define HASH_FILE_VERSION_STRING "2"
 static const u_int32_t HASH_FILE_VERSION=2;
 const int64_t HashManager::MIN_BLOCK_SIZE = 64*1024;
@@ -438,9 +442,9 @@
 	}
 }
 
+#ifdef _WIN32
 #define BUF_SIZE (256*1024)
 
-#ifdef _WIN32
 bool HashManager::Hasher::fastHash(const string& fname, u_int8_t* buf, TigerTree& tth, int64_t size, CRC32Filter* xcrc32) {
 	HANDLE h = INVALID_HANDLE_VALUE;
 	DWORD x, y;
@@ -545,7 +549,59 @@
 	::CloseHandle(h);
 	return ok;
 }
-#endif
+
+#else // _WIN32
+
+static const int64_t BUF_SIZE = 0x1000000 - (0x1000000 % getpagesize());
+
+bool HashManager::Hasher::fastHash(const string& filename, u_int8_t* , TigerTree& tth, int64_t size, CRC32Filter* xcrc32) {
+       int fd = open(filename.c_str(), O_RDONLY | O_LARGEFILE);
+       if(fd == -1)
+              return false;
+
+       int64_t size_left = size;
+       int64_t pos = 0;
+       int64_t size_read = 0;
+       void *buf = 0;
+
+       u_int32_t lastRead = GET_TICK();
+       while(pos < size) {
+              size_read = std::min(size_left, BUF_SIZE);
+              buf = mmap(0, size_read, PROT_READ, MAP_SHARED, fd, pos);
+              if(buf == MAP_FAILED) {
+                     close(fd);
+                     return false;
+              }
+
+              madvise(buf, size_read, MADV_SEQUENTIAL | MADV_WILLNEED);
+
+              if(SETTING(MAX_HASH_SPEED) > 0) {
+                     u_int32_t now = GET_TICK();
+                     u_int32_t minTime = size_read * 1000LL / (SETTING(MAX_HASH_SPEED) * 1024LL * 1024LL);
+                     if(lastRead + minTime > now) {
+                            u_int32_t diff = now - lastRead;
+                            Thread::sleep(minTime - diff);
+                     } 
+                     lastRead = lastRead + minTime;
+              } else {
+                     lastRead = GET_TICK();
+              }
+
+              tth.update(buf, size_read);
+              if(xcrc32)
+                     (*xcrc32)(buf, size_read);
+              munmap(buf, size_read);
+          		{
+          			Lock l(cs);
+          			currentSize = max(currentSize - size_read, _LL(0));
+          		}
+              pos += size_read;
+              size_left -= size_read;
+       }
+       return true;
+}
+
+#endif // _WIN32
 
 int HashManager::Hasher::run() {
 	setThreadPriority(Thread::IDLE);
@@ -607,13 +663,15 @@
 					xcrc32 = &crc32;
 
 				size_t n = 0;
-#ifdef _WIN32
 				TigerTree fastTTH(bs);
 				tth = &fastTTH;
+#ifdef _WIN32
 				if(!virtualBuf || !fastHash(fname, buf, fastTTH, size, xcrc32)) {
+#else
+        if(!BOOLSETTING(FAST_HASHING) || !fastHash(fname, 0, fastTTH, size, xcrc32)) {
+#endif				  
 					tth = &slowTTH;
 					crc32 = CRC32Filter();
-#endif
 					u_int32_t lastRead = GET_TICK();
 
 					do {
@@ -638,11 +696,9 @@
 						}
 						sizeLeft -= n;
 					} while (n > 0 && !stop);
-#ifdef _WIN32
 				} else {
 					sizeLeft = 0;
 				}
-#endif
 				f.close();
 				tth->finalize();
 				u_int32_t end = GET_TICK();
Index: client/HashManager.h
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/client/HashManager.h,v
retrieving revision 1.5
diff -u -r1.5 HashManager.h
--- client/HashManager.h	15 Jun 2006 18:30:50 -0000	1.5
+++ client/HashManager.h	9 Sep 2006 22:58:51 -0000
@@ -139,9 +139,7 @@
 		}
 
 		virtual int run();
-#ifdef _WIN32
 		bool fastHash(const string& fname, u_int8_t* buf, TigerTree& tth, int64_t size, CRC32Filter* xcrc32);
-#endif
 		void getStats(string& curFile, int64_t& bytesLeft, size_t& filesLeft) {
 			Lock l(cs);
 			curFile = currentFile;
Index: client/SettingsManager.cpp
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/client/SettingsManager.cpp,v
retrieving revision 1.5
diff -u -r1.5 SettingsManager.cpp
--- client/SettingsManager.cpp	15 Jun 2006 18:30:50 -0000	1.5
+++ client/SettingsManager.cpp	9 Sep 2006 22:58:51 -0000
@@ -73,7 +73,7 @@
 	"NoIpOverride", "SearchOnlyFreeSlots", "LastSearchType", "BoldFinishedDownloads", "BoldFinishedUploads", "BoldQueue", 
 	"BoldHub", "BoldPm", "BoldSearch", "SocketInBuffer", "SocketOutBuffer", "OnlyDlTthFiles", 
 	"OpenWaitingUsers", "BoldWaitingUsers", "OpenSystemLog", "BoldSystemLog", "AutoRefreshTime",
-	"UseSsl", "AutoSearchLimit", "AltSortOrder", "AutoKickNoFavs", "PromptPassword",
+	"UseSsl", "AutoSearchLimit", "AltSortOrder", "AutoKickNoFavs", "PromptPassword", "FastHashing",
 	"SENTRY",
 	// Int64
 	"TotalUpload", "TotalDownload",
@@ -257,6 +257,7 @@
 	setDefault(ALT_SORT_ORDER, false);
 	setDefault(AUTO_KICK_NO_FAVS, false);
 	setDefault(PROMPT_PASSWORD, false);
+	setDefault(FAST_HASHING, true);
 
 #ifdef _WIN32
 	setDefault(MAIN_WINDOW_STATE, SW_SHOWNORMAL);
Index: client/SettingsManager.h
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/client/SettingsManager.h,v
retrieving revision 1.5
diff -u -r1.5 SettingsManager.h
--- client/SettingsManager.h	15 Jun 2006 18:30:50 -0000	1.5
+++ client/SettingsManager.h	9 Sep 2006 22:58:51 -0000
@@ -89,7 +89,7 @@
 		NO_IP_OVERRIDE, SEARCH_ONLY_FREE_SLOTS, LAST_SEARCH_TYPE, BOLD_FINISHED_DOWNLOADS, BOLD_FINISHED_UPLOADS, BOLD_QUEUE, 
 		BOLD_HUB, BOLD_PM, BOLD_SEARCH, SOCKET_IN_BUFFER, SOCKET_OUT_BUFFER, ONLY_DL_TTH_FILES,
 		OPEN_WAITING_USERS, BOLD_WAITING_USERS, OPEN_SYSTEM_LOG, BOLD_SYSTEM_LOG, AUTO_REFRESH_TIME, 
-		USE_SSL, AUTO_SEARCH_LIMIT, ALT_SORT_ORDER, AUTO_KICK_NO_FAVS, PROMPT_PASSWORD,
+		USE_SSL, AUTO_SEARCH_LIMIT, ALT_SORT_ORDER, AUTO_KICK_NO_FAVS, PROMPT_PASSWORD, FAST_HASHING,
 		INT_LAST };
 
 	enum Int64Setting { INT64_FIRST = INT_LAST + 1,
Index: glade/settingsdialog.glade
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/glade/settingsdialog.glade,v
retrieving revision 1.17
diff -u -r1.17 settingsdialog.glade
--- glade/settingsdialog.glade	1 Sep 2006 15:05:56 -0000	1.17
+++ glade/settingsdialog.glade	9 Sep 2006 22:58:51 -0000
@@ -5052,6 +5052,31 @@
 			      <property name="fill">False</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkLabel" id="label217">
+			      <property name="visible">True</property>
+			      <property name="label" translatable="yes"></property>
+			      <property name="use_underline">False</property>
+			      <property name="use_markup">False</property>
+			      <property name="justify">GTK_JUSTIFY_LEFT</property>
+			      <property name="wrap">False</property>
+			      <property name="selectable">False</property>
+			      <property name="xalign">0.5</property>
+			      <property name="yalign">0.5</property>
+			      <property name="xpad">0</property>
+			      <property name="ypad">0</property>
+			      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+			      <property name="width_chars">-1</property>
+			      <property name="single_line_mode">False</property>
+			      <property name="angle">0</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
@@ -5198,6 +5223,25 @@
 			      <property name="fill">False</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkCheckButton" id="fastHashingCheckButton">
+			      <property name="visible">True</property>
+			      <property name="can_focus">True</property>
+			      <property name="label" translatable="yes">fast hashing</property>
+			      <property name="use_underline">True</property>
+			      <property name="relief">GTK_RELIEF_NORMAL</property>
+			      <property name="focus_on_click">True</property>
+			      <property name="active">True</property>
+			      <property name="inconsistent">False</property>
+			      <property name="draw_indicator">True</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
@@ -5386,6 +5430,31 @@
 			      <property name="fill">False</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkLabel" id="label218">
+			      <property name="visible">True</property>
+			      <property name="label" translatable="yes"></property>
+			      <property name="use_underline">False</property>
+			      <property name="use_markup">False</property>
+			      <property name="justify">GTK_JUSTIFY_LEFT</property>
+			      <property name="wrap">False</property>
+			      <property name="selectable">False</property>
+			      <property name="xalign">0.5</property>
+			      <property name="yalign">0.5</property>
+			      <property name="xpad">0</property>
+			      <property name="ypad">0</property>
+			      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+			      <property name="width_chars">-1</property>
+			      <property name="single_line_mode">False</property>
+			      <property name="angle">0</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
@@ -5574,6 +5643,31 @@
 			      <property name="fill">False</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkLabel" id="label219">
+			      <property name="visible">True</property>
+			      <property name="label" translatable="yes"></property>
+			      <property name="use_underline">False</property>
+			      <property name="use_markup">False</property>
+			      <property name="justify">GTK_JUSTIFY_LEFT</property>
+			      <property name="wrap">False</property>
+			      <property name="selectable">False</property>
+			      <property name="xalign">0.5</property>
+			      <property name="yalign">0.5</property>
+			      <property name="xpad">0</property>
+			      <property name="ypad">0</property>
+			      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+			      <property name="width_chars">-1</property>
+			      <property name="single_line_mode">False</property>
+			      <property name="angle">0</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
@@ -5720,6 +5814,31 @@
 			      <property name="fill">False</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkLabel" id="label220">
+			      <property name="visible">True</property>
+			      <property name="label" translatable="yes"></property>
+			      <property name="use_underline">False</property>
+			      <property name="use_markup">False</property>
+			      <property name="justify">GTK_JUSTIFY_LEFT</property>
+			      <property name="wrap">False</property>
+			      <property name="selectable">False</property>
+			      <property name="xalign">0.5</property>
+			      <property name="yalign">0.5</property>
+			      <property name="xpad">0</property>
+			      <property name="ypad">0</property>
+			      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+			      <property name="width_chars">-1</property>
+			      <property name="single_line_mode">False</property>
+			      <property name="angle">0</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
@@ -5908,6 +6027,31 @@
 			      <property name="fill">False</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkLabel" id="label221">
+			      <property name="visible">True</property>
+			      <property name="label" translatable="yes"></property>
+			      <property name="use_underline">False</property>
+			      <property name="use_markup">False</property>
+			      <property name="justify">GTK_JUSTIFY_LEFT</property>
+			      <property name="wrap">False</property>
+			      <property name="selectable">False</property>
+			      <property name="xalign">0.5</property>
+			      <property name="yalign">0.5</property>
+			      <property name="xpad">0</property>
+			      <property name="ypad">0</property>
+			      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+			      <property name="width_chars">-1</property>
+			      <property name="single_line_mode">False</property>
+			      <property name="angle">0</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
Index: linux/settingsdialog.cc
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/linux/settingsdialog.cc,v
retrieving revision 1.33
diff -u -r1.33 settingsdialog.cc
--- linux/settingsdialog.cc	1 Sep 2006 15:05:57 -0000	1.33
+++ linux/settingsdialog.cc	9 Sep 2006 22:58:51 -0000
@@ -274,6 +274,7 @@
 		sm->set(SettingsManager::SOCKET_IN_BUFFER, Util::toString(gtk_spin_button_get_value(GTK_SPIN_BUTTON(getWidget("socketReadSpinButton")))));
 		sm->set(SettingsManager::SOCKET_OUT_BUFFER, Util::toString(gtk_spin_button_get_value(GTK_SPIN_BUTTON(getWidget("socketWriteSpinButton")))));
 		WSET("default-charset", Text::acpToUtf8(string(gtk_entry_get_text(GTK_ENTRY(getWidget("encodingEntry"))))));
+		sm->set(SettingsManager::FAST_HASHING, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(getWidget("fastHashingCheckButton"))));
 	}
 
 	sm->save();
@@ -746,6 +747,7 @@
 		gtk_spin_button_set_value(GTK_SPIN_BUTTON(getWidget("socketReadSpinButton")), (double)SETTING(SOCKET_IN_BUFFER));
 		gtk_spin_button_set_value(GTK_SPIN_BUTTON(getWidget("socketWriteSpinButton")), (double)SETTING(SOCKET_OUT_BUFFER));
 		gtk_entry_set_text(GTK_ENTRY(getWidget("encodingEntry")), WGETS("default-charset").c_str());
+		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(getWidget("fastHashingCheckButton")), SETTING(FAST_HASHING));
 	}
 }