079f5b930be84649612111b959eaed36bef8c1de
braney
  Wed Jun 19 14:38:27 2019 -0700
some tweaks to trackDb caching

diff --git src/hg/lib/trackDbCustom.c src/hg/lib/trackDbCustom.c
index 1d7d0a7..f66a738 100644
--- src/hg/lib/trackDbCustom.c
+++ src/hg/lib/trackDbCustom.c
@@ -1709,32 +1709,32 @@
         {
         // if we can't stat the shared memory, let's just toss it
         cacheLog("can't stat file %s, unlinking", fileName);
         unlink(fileName);
         continue;
         }
 
     if (statBuf.st_mtime < time)
         {
         // if the cache is older than the data, toss it
         cacheLog("cache is older than source, unlinking");
         unlink(fileName);
         continue;
         }
 
-    int oflags = O_RDWR;
-    int fd = shm_open(sharedMemoryName, oflags, 0666 );
+    int oflags = O_RDONLY;
+    int fd = open(fileName, oflags);
     if (fd < 0)
         {
         cacheLog("cannot open %s", sharedMemoryName);
         continue;
         }
 
     unsigned long address = atoi(files->name); // the name of the file is the address it uses
     unsigned long size = fileSize(fileName);
 
     u_char *mem = (u_char *) mmap((void *)address, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
     cacheLog("asked for memory %lx of size %ld, got %lx",address, size, mem);
 
     if ((unsigned long)mem == address)  // make sure we can get this address
         {
         u_char *ret = mem + lmBlockHeaderSize();
@@ -1778,40 +1778,40 @@
 safef(dirName, sizeof dirName, "/dev/shm/trackDbCache/%s", string);
 
 if (!isDirectory(dirName))
     {
     cacheLog("making directory %s", dirName);
     makeDir(dirName);
     chmod(dirName, 0777);
     }
 
 char sharedMemoryName[4096];
 safef(sharedMemoryName, sizeof sharedMemoryName, "trackDbCache/%s",  rTempName(string, "temp", ""));
 
 char tempFileName[4096];
 safef(tempFileName, sizeof tempFileName, "/dev/shm/%s",  sharedMemoryName);
 
-int fd = shm_open(sharedMemoryName, oflags, 0666 );
+int fd = open(tempFileName, oflags, 0666 );
 if (fd < 0)
     {
     unlink(tempFileName);
-    cacheLog("unable to oen shared memory %s errno %d", sharedMemoryName, errno);
+    cacheLog("unable to open shared memory %s errno %d", tempFileName, errno);
     return;
     }
 else
     {
-    cacheLog("oen shared memory %s", sharedMemoryName);
+    cacheLog("open shared memory %s", tempFileName);
     }
 ftruncate(fd, 0);
 ftruncate(fd, size);
 
 size_t psize = getpagesize();
 unsigned long pageMask = psize - 1;
 
 // we should choose an address semi-randomly and make sure we can grab it rather than assume we can
 unsigned long address;
 unsigned long paddress;
 unsigned char *mem;
 for(address = 0x7000000; address < 0xf000000; address += 0x500000)
     {
     paddress = (address + psize - 1) & ~pageMask;
 
@@ -1820,52 +1820,54 @@
     if ((unsigned long)mem == address)
         break;
     cacheLog("unmapping memory at %lx",mem);
     munmap((void *)mem, size);
     }
 
 if (address >= 0xf000000)
     errAbort("can't get address");
 
 struct lm *lm = lmInitWMem(mem, size);
 struct hash *superHash = newHash(8);
 
 lmCloneTdbList(lm, list, NULL, superHash);
 
 unsigned long memUsed = lmUsed(lm) + lmBlockHeaderSize();
-cacheLog("cloning tdbList using %ld bytes",memUsed);
+cacheLog("cloning tdbList %p used %ld bytes called with %ld", list, memUsed, size);
 
 msync((void *)paddress, memUsed, MS_SYNC);
 ftruncate(fd, memUsed);
 
 // for the moment we're not unmapping these so multiple attached hubs will get
 // different addresses
 //munmap((void *)paddress, size);
 //close(fd);
 
 char fileName[4096];
 safef(fileName, sizeof fileName, "/dev/shm/trackDbCache/%s/%ld",  string, paddress);
 
 cacheLog("renaming %s to %s", tempFileName, fileName);
 mustRename(tempFileName, fileName);
 }
 
 void trackDbCloneTdbListToSharedMem(char *db, struct trackDb *list, unsigned long size)
 /* For this native db, allocate shared memory and clone trackDb list into it. */
 {
 cacheLog("cloning memory for db %s %ld", db, size);
 cloneTdbListToSharedMem(db, list, size);
 }
 
 void trackDbHubCloneTdbListToSharedMem(char *trackDbUrl, struct trackDb *list, unsigned long size)
 /* For this hub, Allocate shared memory and clone trackDb list into it. */
 {
+if ((*trackDbUrl == '.') || (list == NULL)) // don't cache empty lists or collections
+    return;
 cacheLog("cloning memory for hub %s %ld", trackDbUrl, size);
 unsigned char hash[SHA_DIGEST_LENGTH];
 SHA1((const unsigned char *)trackDbUrl, strlen(trackDbUrl), hash);
 
 char newName[(SHA_DIGEST_LENGTH + 1) * 2];
 hexBinaryString(hash,  SHA_DIGEST_LENGTH, newName, (SHA_DIGEST_LENGTH + 1) * 2);
 
 cloneTdbListToSharedMem(newName, list, size);
 }