NCBI C++ Toolkit Cross Reference

  C++/src/objtools/blast/seqdb_reader/seqdbatlas.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
/* $Id: seqdbatlas.cpp 78040 2017-05-22 15:48:20Z zaretska $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Kevin Bealer * */ /// @file seqdbatlas.cpp /// Implementation for the CSeqDBAtlas class and several related /// classes, which provide control of a set of memory mappings. #include <ncbi_pch.hpp> #include <objtools/blast/seqdb_reader/impl/seqdbatlas.hpp> #include <objtools/blast/seqdb_reader/impl/seqdbgeneral.hpp> #include <memory> #include <algorithm> #include <objtools/blast/seqdb_reader/seqdbcommon.hpp> #include <corelib/ncbi_system.hpp> #if defined(NCBI_OS_UNIX) #include <unistd.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/resource.h> #include <unistd.h> #endif BEGIN_NCBI_SCOPE #ifdef SEQDB_TRACE_LOGFILE // By default, the first 16 trace classes are enabled ofstream * seqdb_logfile = 0; int seqdb_logclass = 0xFFFF; void seqdb_log(const char * s) { seqdb_log(1, s); } void seqdb_log(const char * s1, const string & s2) { seqdb_log(1, s1, s2); } inline bool seqdb_log_disabled(int cl) { return ! (seqdb_logfile && (cl & seqdb_logclass)); } void seqdb_log(int cl, const char * s) { if (seqdb_log_disabled(cl)) return; (*seqdb_logfile) << s << endl; } void seqdb_log(int cl, const char * s1, const string & s2) { if (seqdb_log_disabled(cl)) return; (*seqdb_logfile) << s1 << s2 << endl; } void seqdb_log(int cl, const char * s1, int s2) { if (seqdb_log_disabled(cl)) return; (*seqdb_logfile) << s1 << s2 << endl; } void seqdb_log(int cl, const char * s1, int s2, const char * s3) { if (seqdb_log_disabled(cl)) return; (*seqdb_logfile) << s1 << s2 << s3 << endl; } void seqdb_log(int cl, const char * s1, int s2, const char * s3, int s4) { if (seqdb_log_disabled(cl)) return; (*seqdb_logfile) << s1 << s2 << s3 << s4 << endl; } void seqdb_log(int cl, const char * s1, int s2, const char * s3, int s4, const char * s5, int s6) { if (seqdb_log_disabled(cl)) return; (*seqdb_logfile) << s1 << s2 << s3 << s4 << s5 << s6 << endl; } #endif // SEQDB_TRACE_LOGFILE // Further optimizations: // 1. Regions could be stored in a map<>, sorted by file, then offset. // This would allow a binary search instead of sequential and would // vastly improve the "bad case" of 100_000s of buffers of file data. // 2. "Scrounging" could be done in the file case. It is bad to read // 0-4096 then 4096 to 8192, then 4000-4220. The third could use the // first two to avoid reading. It should either combine the first two // regions into a new region, or else just copy to a new region and // leave the old ones alone (possibly marking the old regions as high // penalty). Depending on refcnt, penalty, and region sizes. // Throw function void SeqDB_ThrowException(CSeqDBException::EErrCode code, const string & msg) { switch(code) { case CSeqDBException::eArgErr: NCBI_THROW(CSeqDBException, eArgErr, msg); case CSeqDBException::eFileErr: NCBI_THROW(CSeqDBException, eFileErr, msg); default: NCBI_THROW(CSeqDBException, eMemErr, msg); } } /// Build and throw a file-not-found exception. /// /// @param fname The name of the unfound file. [in] static void s_SeqDB_FileNotFound(const string & fname) { string msg("File ["); msg += fname; msg += "] not found."; SeqDB_ThrowException(CSeqDBException::eFileErr, msg); } /// Check the size of a number relative to the scope of a numeric type. template<class TIn, class TOut> TOut SeqDB_CheckLength(TIn value) { TOut result = TOut(value); if (sizeof(TOut) < sizeof(TIn)) { if (TIn(result) != value) { SeqDB_ThrowException(CSeqDBException::eFileErr, "Offset type does not span file length."); } } return result; } CSeqDBAtlas::CSeqDBAtlas(bool use_mmap) :m_CurAlloc (0), m_LastFID (0), m_MaxFileSize (0), m_SearchPath (GenerateSearchPath()) { m_Alloc = false; m_OpenedFilesCount = 0; m_MaxOpenedFilesCount = 0; } CSeqDBAtlas::~CSeqDBAtlas() { //int openedFilesCount = GetOpenedFilseCount(); //cerr << "********Before Cleaning: openedFilesCount=" << openedFilesCount << endl; for (map<string, CMemoryFile *>::iterator it=m_FileMemMap.begin(); it!=m_FileMemMap.end(); ++it) { string filename = it->first; it->second->Unmap(); //ChangeOpenedFilseCount(eFileCounterDecrement); //cerr << "********Cleaning:Unmap CMemoryFile:" << filename << endl; delete it->second; } //openedFilesCount = GetOpenedFilseCount(); //cerr << "********After Cleaning: openedFilesCount=" << openedFilesCount << " maxOpenedFilesCount="<< m_MaxOpenedFilesCount << endl; // For now, and maybe permanently, enforce balance. _ASSERT(m_Pool.size() == 0); // Erase 'manually allocated' elements - In debug mode, this will // not execute, because of the above test. for(TPoolIter i = m_Pool.begin(); i != m_Pool.end(); i++) { delete[] (char*)((*i).first); } m_Pool.clear(); } bool CSeqDBAtlas::DoesFileExist(const string & fname, CSeqDBLockHold & locked) { TIndx length(0); return GetFileSize(fname, length, locked); } bool CSeqDBAtlas::GetFileSize(const string & fname, TIndx & length, CSeqDBLockHold & locked) { Lock(locked); return GetFileSizeL(fname, length); } bool CSeqDBAtlas::GetFileSizeL(const string & fname, TIndx & length) { // Fields: file-exists, file-length pair<bool, TIndx> data; map< string, pair<bool, TIndx> >::iterator i = m_FileSize.find(fname); if (i == m_FileSize.end()) { CFile whole(fname); Int8 file_length = whole.GetLength(); if (file_length >= 0) { data.first = true; data.second = SeqDB_CheckLength<Int8,TIndx>(file_length); if ((Uint8)file_length > m_MaxFileSize) m_MaxFileSize = file_length; } else { data.first = false; data.second = 0; } m_FileSize[fname] = data; } else { data = (*i).second; } length = data.second; return data.first; } /// Simple idiom for RIIA with malloc + free. struct CSeqDBAutoFree { /// Constructor. CSeqDBAutoFree() : m_Array(0) { } /// Specify a malloced area of memory. void Set(const char * x) { m_Array = x; } /// Destructor will free that memory. ~CSeqDBAutoFree() { if (m_Array) { free((void*) m_Array); } } private: /// Pointer to malloced memory. const char * m_Array; }; /// Releases allocated memory void CSeqDBAtlas::x_RetRegion(const char * datap) { bool worked = x_Free(datap); _ASSERT(worked); if (! worked) { cerr << "Address leak in CSeqDBAtlas::RetRegion" << endl; } } char * CSeqDBAtlas::Alloc(size_t length, CSeqDBLockHold & locked, bool clear) { // What should/will happen on allocation failure? Lock(locked); if (! length) { length = 1; } // Allocate/clear char * newcp = 0; try { newcp = new char[length]; // new() should have thrown, but some old implementations are // said to be non-compliant in this regard: if (! newcp) { throw std::bad_alloc(); } if (clear) { memset(newcp, 0, length); } } catch(std::bad_alloc) { NCBI_THROW(CSeqDBException, eMemErr, "CSeqDBAtlas::Alloc: allocation failed."); } // Add to pool. _ASSERT(m_Pool.find(newcp) == m_Pool.end()); m_Pool[newcp] = length; m_CurAlloc += length; m_Alloc = true; //cerr << "allocated " << m_CurAlloc << " memory" << endl; return newcp; } void CSeqDBAtlas::Free(const char * freeme, CSeqDBLockHold & locked) { Lock(locked); #ifdef _DEBUG bool found = x_Free(freeme); _ASSERT(found); #else x_Free(freeme); #endif } bool CSeqDBAtlas::x_Free(const char * freeme) { if(!m_Alloc) return true; TPoolIter i = m_Pool.find((const char*) freeme); if (i == m_Pool.end()) { return false; } size_t sz = (*i).second; _ASSERT(m_CurAlloc >= (TIndx)sz); m_CurAlloc -= sz; //cerr << "deallocated " << sz << " memory m_CurAlloc=" << m_CurAlloc << endl; if(m_CurAlloc == 0) m_Alloc = false; char * cp = (char*) freeme; delete[] cp; m_Pool.erase(i); return true; } void CSeqDBAtlas::RegisterExternal(CSeqDBMemReg & memreg, size_t bytes, CSeqDBLockHold & locked) { if (bytes > 0) { Lock(locked); _ASSERT(memreg.m_Bytes == 0); memreg.m_Bytes = bytes; } } void CSeqDBAtlas::UnregisterExternal(CSeqDBMemReg & memreg) { size_t bytes = memreg.m_Bytes; if (bytes > 0) { memreg.m_Bytes = 0; } } CSeqDBAtlasHolder::CSeqDBAtlasHolder(bool use_mmap, CSeqDBLockHold * lockedp) { {{ CFastMutexGuard guard(m_Lock); if (m_Count == 0) { m_Atlas = new CSeqDBAtlas(use_mmap); } m_Count ++; }} } DEFINE_CLASS_STATIC_FAST_MUTEX(CSeqDBAtlasHolder::m_Lock); CSeqDBAtlasHolder::~CSeqDBAtlasHolder() { CFastMutexGuard guard(m_Lock); m_Count --; if (m_Count == 0) { delete m_Atlas; } } CSeqDBAtlas & CSeqDBAtlasHolder::Get() { _ASSERT(m_Atlas); return *m_Atlas; } CSeqDBLockHold::~CSeqDBLockHold() { CHECK_MARKER(); m_Atlas.Unlock(*this); BREAK_MARKER(); } int CSeqDBAtlasHolder::m_Count = 0; CSeqDBAtlas * CSeqDBAtlasHolder::m_Atlas = NULL; CSeqDB_AtlasRegionHolder:: CSeqDB_AtlasRegionHolder(CSeqDBAtlas & atlas, const char * ptr) : m_Atlas(atlas), m_Ptr(ptr) { } CSeqDB_AtlasRegionHolder::~CSeqDB_AtlasRegionHolder() { if (m_Ptr) { CSeqDBLockHold locked(m_Atlas); m_Atlas.Lock(locked); //m_Atlas.RetRegion(m_Ptr); m_Ptr = NULL; } } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.