CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
talk.cc
Go to the documentation of this file.
1 
15 #ifndef __STDC_FORMAT_MACROS
16 #define __STDC_FORMAT_MACROS
17 #endif
18 
19 #include "cvmfs_config.h"
20 #include "talk.h"
21 
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <pthread.h>
25 #include <stdint.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/uio.h>
30 #include <sys/un.h>
31 #include <unistd.h>
32 
33 #include <cassert>
34 #include <cstdlib>
35 #include <cstring>
36 #include <string>
37 #include <vector>
38 
39 #include "cache.h"
40 #include "cache_posix.h"
41 #include "catalog_mgr_client.h"
42 #include "cvmfs.h"
43 #include "download.h"
44 #include "duplex_sqlite3.h"
45 #include "fuse_remount.h"
46 #include "glue_buffer.h"
47 #include "loader.h"
48 #include "logging.h"
49 #include "monitor.h"
50 #include "mountpoint.h"
51 #include "nfs_maps.h"
52 #include "options.h"
53 #include "platform.h"
54 #include "quota.h"
55 #include "shortstring.h"
56 #include "statistics.h"
57 #include "tracer.h"
58 #include "util/pointer.h"
59 #include "wpad.h"
60 
61 using namespace std; // NOLINT
62 
63 
64 void TalkManager::Answer(int con_fd, const string &msg) {
65  (void)send(con_fd, &msg[0], msg.length(), MSG_NOSIGNAL);
66 }
67 
68 
69 void TalkManager::AnswerStringList(int con_fd, const vector<string> &list) {
70  string list_str;
71  for (unsigned i = 0; i < list.size(); ++i) {
72  list_str += list[i] + "\n";
73  }
74  Answer(con_fd, list_str);
75 }
76 
77 
79  const string &socket_path,
80  MountPoint *mount_point,
81  FuseRemounter *remounter)
82 {
84  talk_manager(new TalkManager(socket_path, mount_point, remounter));
85 
86  talk_manager->socket_fd_ = MakeSocket(socket_path, 0660);
87  if (talk_manager->socket_fd_ == -1)
88  return NULL;
89  if (listen(talk_manager->socket_fd_, 1) == -1)
90  return NULL;
91 
92  LogCvmfs(kLogTalk, kLogDebug, "socket created at %s (fd %d)",
93  socket_path.c_str(), talk_manager->socket_fd_);
94 
95  return talk_manager.Release();
96 }
97 
98 
100  vector<string> host_chain;
101  vector<int> rtt;
102  unsigned active_host;
103 
104  download_mgr->GetHostInfo(&host_chain, &rtt, &active_host);
105  string host_str;
106  for (unsigned i = 0; i < host_chain.size(); ++i) {
107  host_str += " [" + StringifyInt(i) + "] " + host_chain[i] + " (";
109  host_str += "unprobed";
110  else if (rtt[i] == download::DownloadManager::kProbeDown)
111  host_str += "host down";
112  else if (rtt[i] == download::DownloadManager::kProbeGeo)
113  host_str += "geographically ordered";
114  else
115  host_str += StringifyInt(rtt[i]) + " ms";
116  host_str += ")\n";
117  }
118  host_str += "Active host " + StringifyInt(active_host) + ": " +
119  host_chain[active_host] + "\n";
120  return host_str;
121 }
122 
124  vector< vector<download::DownloadManager::ProxyInfo> > proxy_chain;
125  unsigned active_group;
126  unsigned fallback_group;
127 
128  download_mgr->GetProxyInfo(&proxy_chain, &active_group, &fallback_group);
129  string proxy_str;
130  if (proxy_chain.size()) {
131  proxy_str += "Load-balance groups:\n";
132  for (unsigned i = 0; i < proxy_chain.size(); ++i) {
133  vector<string> urls;
134  for (unsigned j = 0; j < proxy_chain[i].size(); ++j) {
135  urls.push_back(proxy_chain[i][j].Print());
136  }
137  proxy_str +=
138  "[" + StringifyInt(i) + "] " + JoinStrings(urls, ", ") + "\n";
139  }
140  proxy_str += "Active proxy: [" + StringifyInt(active_group) + "] " +
141  proxy_chain[active_group][0].url + "\n";
142  if (fallback_group < proxy_chain.size())
143  proxy_str += "First fallback group: [" +
144  StringifyInt(fallback_group) + "]\n";
145  } else {
146  proxy_str = "No proxies defined\n";
147  }
148  return proxy_str;
149 }
150 
151 
156 void *TalkManager::MainResponder(void *data) {
157  TalkManager *talk_mgr = reinterpret_cast<TalkManager *>(data);
158  MountPoint *mount_point = talk_mgr->mount_point_;
159  FileSystem *file_system = mount_point->file_system();
160  FuseRemounter *remounter = talk_mgr->remounter_;
161  LogCvmfs(kLogTalk, kLogDebug, "talk thread started");
162 
163  struct sockaddr_un remote;
164  socklen_t socket_size = sizeof(remote);
165  int con_fd = -1;
166  while (true) {
167  if (con_fd >= 0) {
168  shutdown(con_fd, SHUT_RDWR);
169  close(con_fd);
170  }
171  LogCvmfs(kLogTalk, kLogDebug, "accepting connections on socketfd %d",
172  talk_mgr->socket_fd_);
173  if ((con_fd = accept(talk_mgr->socket_fd_,
174  (struct sockaddr *)&remote,
175  &socket_size)) < 0)
176  {
177  LogCvmfs(kLogTalk, kLogDebug, "terminating talk thread (fd %d, errno %d)",
178  con_fd, errno);
179  break;
180  }
181 
182  char buf[kMaxCommandSize];
183  int bytes_read;
184  if ((bytes_read = recv(con_fd, buf, sizeof(buf), 0)) <= 0)
185  continue;
186 
187  if (buf[bytes_read-1] == '\0')
188  bytes_read--;
189  const string line = string(buf, bytes_read);
190  LogCvmfs(kLogTalk, kLogDebug, "received %s (length %u)",
191  line.c_str(), line.length());
192 
193  if (line == "tracebuffer flush") {
194  mount_point->tracer()->Flush();
195  talk_mgr->Answer(con_fd, "OK\n");
196  } else if (line == "cache size") {
197  QuotaManager *quota_mgr = file_system->cache_mgr()->quota_mgr();
199  talk_mgr->Answer(con_fd, "Cache cannot report its size\n");
200  } else {
201  uint64_t size_unpinned = quota_mgr->GetSize();
202  uint64_t size_pinned = quota_mgr->GetSizePinned();
203  const string size_str = "Current cache size is " +
204  StringifyInt(size_unpinned / (1024*1024)) + "MB (" +
205  StringifyInt(size_unpinned) + " Bytes), pinned: " +
206  StringifyInt(size_pinned / (1024*1024)) + "MB (" +
207  StringifyInt(size_pinned) + " Bytes)\n";
208  talk_mgr->Answer(con_fd, size_str);
209  }
210  } else if (line == "cache instance") {
211  talk_mgr->Answer(con_fd, file_system->cache_mgr()->Describe());
212  } else if (line == "cache list") {
213  QuotaManager *quota_mgr = file_system->cache_mgr()->quota_mgr();
214  if (!quota_mgr->HasCapability(QuotaManager::kCapList)) {
215  talk_mgr->Answer(con_fd, "Cache cannot list its entries\n");
216  } else {
217  vector<string> ls = quota_mgr->List();
218  talk_mgr->AnswerStringList(con_fd, ls);
219  }
220  } else if (line == "cache list pinned") {
221  QuotaManager *quota_mgr = file_system->cache_mgr()->quota_mgr();
222  if (!quota_mgr->HasCapability(QuotaManager::kCapList)) {
223  talk_mgr->Answer(con_fd, "Cache cannot list its entries\n");
224  } else {
225  vector<string> ls_pinned = quota_mgr->ListPinned();
226  talk_mgr->AnswerStringList(con_fd, ls_pinned);
227  }
228  } else if (line == "cache list catalogs") {
229  QuotaManager *quota_mgr = file_system->cache_mgr()->quota_mgr();
230  if (!quota_mgr->HasCapability(QuotaManager::kCapList)) {
231  talk_mgr->Answer(con_fd, "Cache cannot list its entries\n");
232  } else {
233  vector<string> ls_catalogs = quota_mgr->ListCatalogs();
234  talk_mgr->AnswerStringList(con_fd, ls_catalogs);
235  }
236  } else if (line.substr(0, 12) == "cleanup rate") {
237  QuotaManager *quota_mgr = file_system->cache_mgr()->quota_mgr();
239  talk_mgr->Answer(con_fd, "Unsupported by this cache\n");
240  } else {
241  if (line.length() < 14) {
242  talk_mgr->Answer(con_fd, "Usage: cleanup rate <period in mn>\n");
243  } else {
244  const uint64_t period_s = String2Uint64(line.substr(13)) * 60;
245  const uint64_t rate = quota_mgr->GetCleanupRate(period_s);
246  talk_mgr->Answer(con_fd, StringifyInt(rate) + "\n");
247  }
248  }
249  } else if (line.substr(0, 7) == "cleanup") {
250  QuotaManager *quota_mgr = file_system->cache_mgr()->quota_mgr();
251  if (!quota_mgr->HasCapability(QuotaManager::kCapShrink)) {
252  talk_mgr->Answer(con_fd, "Cache cannot trigger eviction\n");
253  } else {
254  if (line.length() < 9) {
255  talk_mgr->Answer(con_fd, "Usage: cleanup <MB>\n");
256  } else {
257  const uint64_t size = String2Uint64(line.substr(8))*1024*1024;
258  if (quota_mgr->Cleanup(size)) {
259  talk_mgr->Answer(con_fd, "OK\n");
260  } else {
261  talk_mgr->Answer(con_fd, "Not fully cleaned "
262  "(there might be pinned chunks)\n");
263  }
264  }
265  }
266  } else if (line.substr(0, 5) == "evict") {
267  assert(mount_point->file_system()->type() == FileSystem::kFsFuse);
268  if (line.length() < 7) {
269  talk_mgr->Answer(con_fd, "Usage: evict <path>\n");
270  } else {
271  const string path = line.substr(6);
272  const bool found_regular = cvmfs::Evict(path);
273  if (found_regular)
274  talk_mgr->Answer(con_fd, "OK\n");
275  else
276  talk_mgr->Answer(con_fd, "No such regular file\n");
277  }
278  } else if (line.substr(0, 3) == "pin") {
279  assert(mount_point->file_system()->type() == FileSystem::kFsFuse);
280  if (line.length() < 5) {
281  talk_mgr->Answer(con_fd, "Usage: pin <path>\n");
282  } else {
283  const string path = line.substr(4);
284  const bool found_regular = cvmfs::Pin(path);
285  if (found_regular)
286  talk_mgr->Answer(con_fd, "OK\n");
287  else
288  talk_mgr->Answer(con_fd, "No such regular file or pinning failed\n");
289  }
290  } else if (line == "mountpoint") {
291  talk_mgr->Answer(con_fd, cvmfs::loader_exports_->mount_point + "\n");
292  } else if (line == "device id") {
293  if (cvmfs::loader_exports_->version >= 5)
294  talk_mgr->Answer(con_fd, cvmfs::loader_exports_->device_id + "\n");
295  else
296  talk_mgr->Answer(con_fd, "0:0\n");
297  } else if (line.substr(0, 13) == "send mount fd") {
298  // Hidden command intended to be used only by the cvmfs mount helper
299  if (line.length() < 15) {
300  talk_mgr->Answer(con_fd, "EINVAL\n");
301  } else {
302  std::string socket_path = line.substr(14);
303  bool retval = cvmfs::SendFuseFd(socket_path);
304  talk_mgr->Answer(con_fd, retval ? "OK\n" : "Failed\n");
306  "Transfer fuse connection to new mount (via %s): %s",
307  socket_path.c_str(), retval ? "success" : "failure");
308  }
309  } else if (line.substr(0, 7) == "remount") {
310  FuseRemounter::Status status;
311  if (line == "remount sync")
312  status = remounter->CheckSynchronously();
313  else
314  status = remounter->Check();
315  switch (status) {
317  talk_mgr->Answer(con_fd, "Failed\n");
318  break;
320  talk_mgr->Answer(con_fd, "Failed (no space)\n");
321  break;
323  talk_mgr->Answer(con_fd, "Catalog up to date\n");
324  break;
326  talk_mgr->Answer(con_fd, "New revision applied\n");
327  break;
329  talk_mgr->Answer(con_fd, "In maintenance mode\n");
330  break;
331  default:
332  talk_mgr->Answer(con_fd, "internal error\n");
333  }
334  } else if (line.substr(0, 6) == "chroot") {
335  if (line.length() < 8) {
336  talk_mgr->Answer(con_fd, "Usage: chroot <hash>\n");
337  } else {
338  std::string root_hash = Trim(line.substr(7), true /* trim_newline */);
339  FuseRemounter::Status status = remounter->ChangeRoot(
341  switch (status) {
343  talk_mgr->Answer(con_fd, "OK\n");
344  break;
345  default:
346  talk_mgr->Answer(con_fd, "Failed\n");
347  break;
348  }
349  }
350  } else if (line == "detach nested catalogs") {
351  mount_point->catalog_mgr()->DetachNested();
352  talk_mgr->Answer(con_fd, "OK\n");
353  } else if (line == "revision") {
354  string revision = StringifyInt(mount_point->catalog_mgr()->GetRevision());
355  talk_mgr->Answer(con_fd, revision + "\n");
356  } else if (line == "max ttl info") {
357  const unsigned max_ttl = mount_point->GetMaxTtlMn();
358  if (max_ttl == 0) {
359  talk_mgr->Answer(con_fd, "unset\n");
360  } else {
361  const string max_ttl_str = StringifyInt(max_ttl) + " minutes\n";
362  talk_mgr->Answer(con_fd, max_ttl_str);
363  }
364  } else if (line.substr(0, 11) == "max ttl set") {
365  if (line.length() < 13) {
366  talk_mgr->Answer(con_fd, "Usage: max ttl set <minutes>\n");
367  } else {
368  const unsigned max_ttl = String2Uint64(line.substr(12));
369  mount_point->SetMaxTtlMn(max_ttl);
370  talk_mgr->Answer(con_fd, "OK\n");
371  }
372  } else if (line.substr(0, 14) == "nameserver get") {
373  const string dns_server = mount_point->download_mgr()->GetDnsServer();
374  const string reply = !dns_server.empty() ?
375  std::string("DNS server address: ") + dns_server + "\n":
376  std::string("DNS server not set.\n");
377  talk_mgr->Answer(con_fd, reply);
378  } else if (line.substr(0, 14) == "nameserver set") {
379  if (line.length() < 16) {
380  talk_mgr->Answer(con_fd, "Usage: nameserver set <host>\n");
381  } else {
382  const string host = line.substr(15);
383  mount_point->download_mgr()->SetDnsServer(host);
384  talk_mgr->Answer(con_fd, "OK\n");
385  }
386  } else if (line == "external host info") {
387  string external_host_info =
388  talk_mgr->FormatHostInfo(mount_point->external_download_mgr());
389  talk_mgr->Answer(con_fd, external_host_info);
390  } else if (line == "host info") {
391  string host_info = talk_mgr->FormatHostInfo(mount_point->download_mgr());
392  talk_mgr->Answer(con_fd, host_info);
393  } else if (line == "host probe") {
394  mount_point->download_mgr()->ProbeHosts();
395  talk_mgr->Answer(con_fd, "OK\n");
396  } else if (line == "host probe geo") {
397  bool retval = mount_point->download_mgr()->ProbeGeo();
398  if (retval)
399  talk_mgr->Answer(con_fd, "OK\n");
400  else
401  talk_mgr->Answer(con_fd, "Failed\n");
402  } else if (line == "external host switch") {
403  mount_point->external_download_mgr()->SwitchHost();
404  talk_mgr->Answer(con_fd, "OK\n");
405  } else if (line == "host switch") {
406  mount_point->download_mgr()->SwitchHost();
407  talk_mgr->Answer(con_fd, "OK\n");
408  } else if (line.substr(0, 17) == "external host set") {
409  if (line.length() < 19) {
410  talk_mgr->Answer(con_fd, "Usage: external host set <URL>\n");
411  } else {
412  const std::string host = line.substr(18);
413  mount_point->external_download_mgr()->SetHostChain(host);
414  talk_mgr->Answer(con_fd, "OK\n");
415  }
416  } else if (line.substr(0, 8) == "host set") {
417  if (line.length() < 10) {
418  talk_mgr->Answer(con_fd, "Usage: host set <host list>\n");
419  } else {
420  const string hosts = line.substr(9);
421  mount_point->download_mgr()->SetHostChain(hosts);
422  talk_mgr->Answer(con_fd, "OK\n");
423  }
424  } else if (line == "external proxy info") {
425  string external_proxy_info =
426  talk_mgr->FormatProxyInfo(mount_point->external_download_mgr());
427  talk_mgr->Answer(con_fd, external_proxy_info);
428  } else if (line == "proxy info") {
429  string proxy_info =
430  talk_mgr->FormatProxyInfo(mount_point->download_mgr());
431  talk_mgr->Answer(con_fd, proxy_info);
432  } else if (line == "proxy rebalance") {
433  mount_point->download_mgr()->RebalanceProxies();
434  talk_mgr->Answer(con_fd, "OK\n");
435  } else if (line == "proxy group switch") {
436  mount_point->download_mgr()->SwitchProxyGroup();
437  talk_mgr->Answer(con_fd, "OK\n");
438  } else if (line.substr(0, 18) == "external proxy set") {
439  if (line.length() < 20) {
440  talk_mgr->Answer(con_fd, "Usage: external proxy set <proxy list>\n");
441  } else {
442  string external_proxies = line.substr(19);
443  mount_point->external_download_mgr()->SetProxyChain(
444  external_proxies, "", download::DownloadManager::kSetProxyRegular);
445  talk_mgr->Answer(con_fd, "OK\n");
446  }
447  } else if (line.substr(0, 9) == "proxy set") {
448  if (line.length() < 11) {
449  talk_mgr->Answer(con_fd, "Usage: proxy set <proxy list>\n");
450  } else {
451  string proxies = line.substr(10);
452  proxies =
454  mount_point->download_mgr());
455  if (proxies == "") {
456  talk_mgr->Answer(con_fd, "Failed, no valid proxies\n");
457  } else {
458  mount_point->download_mgr()->SetProxyChain(
460  talk_mgr->Answer(con_fd, "OK\n");
461  }
462  }
463  } else if (line.substr(0, 14) == "proxy fallback") {
464  if (line.length() < 15) {
465  talk_mgr->Answer(con_fd, "Usage: proxy fallback <proxy list>\n");
466  } else {
467  string fallback_proxies = line.substr(15);
468  mount_point->download_mgr()->SetProxyChain(
469  "", fallback_proxies, download::DownloadManager::kSetProxyFallback);
470  talk_mgr->Answer(con_fd, "OK\n");
471  }
472  } else if (line == "timeout info") {
473  unsigned timeout;
474  unsigned timeout_direct;
475  mount_point->download_mgr()->GetTimeout(&timeout, &timeout_direct);
476  string timeout_str = "Timeout with proxy: ";
477  if (timeout)
478  timeout_str += StringifyInt(timeout) + "s\n";
479  else
480  timeout_str += "no timeout\n";
481  timeout_str += "Timeout without proxy: ";
482  if (timeout_direct)
483  timeout_str += StringifyInt(timeout_direct) + "s\n";
484  else
485  timeout_str += "no timeout\n";
486  talk_mgr->Answer(con_fd, timeout_str);
487  } else if (line.substr(0, 11) == "timeout set") {
488  if (line.length() < 13) {
489  talk_mgr->Answer(con_fd, "Usage: timeout set <proxy> <direct>\n");
490  } else {
491  uint64_t timeout;
492  uint64_t timeout_direct;
493  String2Uint64Pair(line.substr(12), &timeout, &timeout_direct);
494  mount_point->download_mgr()->SetTimeout(timeout, timeout_direct);
495  talk_mgr->Answer(con_fd, "OK\n");
496  }
497  } else if (line == "open catalogs") {
498  talk_mgr->Answer(con_fd, mount_point->catalog_mgr()->PrintHierarchy());
499  } else if (line == "internal affairs") {
500  int current;
501  int highwater;
502  string result;
503 
504  result += "Inode Generation:\n " + cvmfs::PrintInodeGeneration();
505 
506  // Manually setting the values of the ShortString counters
507  mount_point->statistics()->Lookup("pathstring.n_instances")->
509  mount_point->statistics()->Lookup("pathstring.n_overflows")->
511  mount_point->statistics()->Lookup("namestring.n_instances")->
513  mount_point->statistics()->Lookup("namestring.n_overflows")->
515  mount_point->statistics()->Lookup("linkstring.n_instances")->
517  mount_point->statistics()->Lookup("linkstring.n_overflows")->
519 
520  // Manually setting the inode tracker numbers
521  glue::InodeTracker::Statistics inode_stats =
522  mount_point->inode_tracker()->GetStatistics();
523  glue::NentryTracker::Statistics nentry_stats =
524  mount_point->nentry_tracker()->GetStatistics();
525  mount_point->statistics()->Lookup("inode_tracker.n_insert")->Set(
526  atomic_read64(&inode_stats.num_inserts));
527  mount_point->statistics()->Lookup("inode_tracker.n_remove")->Set(
528  atomic_read64(&inode_stats.num_removes));
529  mount_point->statistics()->Lookup("inode_tracker.no_reference")->Set(
530  atomic_read64(&inode_stats.num_references));
531  mount_point->statistics()->Lookup("inode_tracker.n_hit_inode")->Set(
532  atomic_read64(&inode_stats.num_hits_inode));
533  mount_point->statistics()->Lookup("inode_tracker.n_hit_path")->Set(
534  atomic_read64(&inode_stats.num_hits_path));
535  mount_point->statistics()->Lookup("inode_tracker.n_miss_path")->Set(
536  atomic_read64(&inode_stats.num_misses_path));
537  mount_point->statistics()->Lookup("nentry_tracker.n_insert")->Set(
538  nentry_stats.num_insert);
539  mount_point->statistics()->Lookup("nentry_tracker.n_remove")->Set(
540  nentry_stats.num_remove);
541  mount_point->statistics()->Lookup("nentry_tracker.n_prune")->Set(
542  nentry_stats.num_prune);
543 
544  if (file_system->cache_mgr()->id() == kPosixCacheManager) {
545  PosixCacheManager *cache_mgr =
546  reinterpret_cast<PosixCacheManager *>(
547  file_system->cache_mgr());
548  result += "\nCache Mode: ";
549  switch (cache_mgr->cache_mode()) {
551  result += "read-write";
552  break;
554  result += "read-only";
555  break;
556  default:
557  result += "unknown";
558  }
559  }
560  bool drainout_mode;
561  bool maintenance_mode;
562  cvmfs::GetReloadStatus(&drainout_mode, &maintenance_mode);
563  result += "\nDrainout Mode: " + StringifyBool(drainout_mode) + "\n";
564  result += "Maintenance Mode: " + StringifyBool(maintenance_mode) + "\n";
565 
566  if (file_system->IsNfsSource()) {
567  result += "\nNFS Map Statistics:\n";
568  result += file_system->nfs_maps()->GetStatistics();
569  }
570 
571  result += "SQlite Statistics:\n";
572  sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &current, &highwater, 0);
573  result += " Number of allocations " + StringifyInt(current) + "\n";
574 
575  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
576  result += " General purpose allocator " +StringifyInt(current/1024) +
577  " KB / " + StringifyInt(highwater/1024) + " KB\n";
578 
579  sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &current, &highwater, 0);
580  result += " Largest malloc " + StringifyInt(highwater) + " Bytes\n";
581 
582  sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &current, &highwater, 0);
583  result += " Page cache allocations " + StringifyInt(current) + " / " +
584  StringifyInt(highwater) + "\n";
585 
586  sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW,
587  &current, &highwater, 0);
588  result += " Page cache overflows " + StringifyInt(current/1024) +
589  " KB / " + StringifyInt(highwater/1024) + " KB\n";
590 
591  sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &current, &highwater, 0);
592  result += " Largest page cache allocation " + StringifyInt(highwater) +
593  " Bytes\n";
594 
595  sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &current, &highwater, 0);
596  result += " Scratch allocations " + StringifyInt(current) + " / " +
597  StringifyInt(highwater) + "\n";
598 
599  sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &current, &highwater, 0);
600  result += " Scratch overflows " + StringifyInt(current) + " / " +
601  StringifyInt(highwater) + "\n";
602 
603  sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &current, &highwater, 0);
604  result += " Largest scratch allocation " + StringifyInt(highwater/1024)
605  + " KB\n";
606 
607  result += "\nPer-Connection Memory Statistics:\n" +
608  mount_point->catalog_mgr()->PrintAllMemStatistics();
609 
610  result += "\nLatency distribution of system calls:\n";
611 
612  result += "Lookup\n" + file_system->hist_fs_lookup()->ToString();
613  result += "Forget\n" + file_system->hist_fs_forget()->ToString();
614  result += "Multi-Forget\n"
615  + file_system->hist_fs_forget_multi()->ToString();
616  result += "Getattr\n" + file_system->hist_fs_getattr()->ToString();
617  result += "Readlink\n" + file_system->hist_fs_readlink()->ToString();
618  result += "Opendir\n" + file_system->hist_fs_opendir()->ToString();
619  result += "Releasedir\n" + file_system->hist_fs_releasedir()->ToString();
620  result += "Readdir\n" + file_system->hist_fs_readdir()->ToString();
621  result += "Open\n" + file_system->hist_fs_open()->ToString();
622  result += "Read\n" + file_system->hist_fs_read()->ToString();
623  result += "Release\n" + file_system->hist_fs_release()->ToString();
624 
625  result += "\nRaw Counters:\n" +
627 
628  talk_mgr->Answer(con_fd, result);
629  } else if (line == "reset error counters") {
630  file_system->ResetErrorCounters();
631  talk_mgr->Answer(con_fd, "OK\n");
632  } else if (line == "pid") {
633  const string pid_str = StringifyInt(cvmfs::pid_) + "\n";
634  talk_mgr->Answer(con_fd, pid_str);
635  } else if (line == "pid cachemgr") {
636  const string pid_str =
637  StringifyInt(file_system->cache_mgr()->quota_mgr()->GetPid()) + "\n";
638  talk_mgr->Answer(con_fd, pid_str);
639  } else if (line == "pid watchdog") {
640  const string pid_str = StringifyInt(Watchdog::GetPid()) + "\n";
641  talk_mgr->Answer(con_fd, pid_str);
642  } else if (line == "parameters") {
643  talk_mgr->Answer(con_fd, file_system->options_mgr()->Dump());
644  } else if (line == "hotpatch history") {
645  string history_str =
646  StringifyTime(cvmfs::loader_exports_->boot_time, true) +
647  " (start of CernVM-FS loader " +
649  for (loader::EventList::const_iterator i =
651  iEnd = cvmfs::loader_exports_->history.end(); i != iEnd; ++i)
652  {
653  history_str += StringifyTime((*i)->timestamp, true) +
654  " (loaded CernVM-FS Fuse Module " +
655  (*i)->so_version + ")\n";
656  }
657  talk_mgr->Answer(con_fd, history_str);
658  } else if (line == "vfs inodes") {
659  string result;
661  mount_point->inode_tracker()->BeginEnumerate());
662  uint64_t inode;
663  while (mount_point->inode_tracker()->NextInode(&cursor, &inode)) {
664  result += StringifyInt(inode) + "\n";
665  }
666  mount_point->inode_tracker()->EndEnumerate(&cursor);
667  talk_mgr->Answer(con_fd, result);
668  } else if (line == "vfs entries") {
669  string result;
671  mount_point->inode_tracker()->BeginEnumerate());
672  uint64_t inode_parent;
673  NameString name;
674  while (mount_point->inode_tracker()->NextEntry(
675  &cursor, &inode_parent, &name))
676  {
677  result += "<" + StringifyInt(inode_parent) + ">/" + name.ToString() +
678  "\n";
679  }
680  mount_point->inode_tracker()->EndEnumerate(&cursor);
681  talk_mgr->Answer(con_fd, result);
682  } else if (line == "version") {
683  string version_str = string(VERSION) + " (CernVM-FS Fuse Module)\n" +
684  cvmfs::loader_exports_->loader_version + " (Loader)\n";
685  talk_mgr->Answer(con_fd, version_str);
686  } else if (line == "version patchlevel") {
687  talk_mgr->Answer(con_fd, string(CVMFS_PATCH_LEVEL) + "\n");
688  } else if (line == "tear down to read-only") {
689  if (file_system->cache_mgr()->id() != kPosixCacheManager) {
690  talk_mgr->Answer(con_fd, "not supported\n");
691  } else {
692  // hack
694  file_system->TearDown2ReadOnly();
695  talk_mgr->Answer(con_fd, "In read-only mode\n");
696  }
697  } else if (line == "latency") {
698  string result = talk_mgr->FormatLatencies(*mount_point, file_system);
699  talk_mgr->Answer(con_fd, result);
700  } else {
701  talk_mgr->Answer(con_fd, "unknown command\n");
702  }
703  }
704 
705  return NULL;
706 } // NOLINT(readability/fn_size)
707 
708 string TalkManager::FormatLatencies(const MountPoint &mount_point,
709  FileSystem *file_system) {
710  string result;
711  const unsigned int bufSize = 300;
712  char buffer[bufSize];
713 
714  vector<float> qs;
715  qs.push_back(.1);
716  qs.push_back(.2);
717  qs.push_back(.25);
718  qs.push_back(.3);
719  qs.push_back(.4);
720  qs.push_back(.5);
721  qs.push_back(.6);
722  qs.push_back(.7);
723  qs.push_back(.75);
724  qs.push_back(.8);
725  qs.push_back(.9);
726  qs.push_back(.95);
727  qs.push_back(.99);
728  qs.push_back(.999);
729  qs.push_back(.9999);
730 
731  string repo(mount_point.fqrn());
732 
733  unsigned int format_index =
734  snprintf(buffer, bufSize, "\"%s\",\"%s\",\"%s\",\"%s\"", "repository",
735  "action", "total_count", "time_unit");
736  for (unsigned int i = 0; i < qs.size(); i++) {
737  format_index += snprintf(buffer + format_index, bufSize - format_index,
738  ",%0.5f", qs[i]);
739  }
740  format_index += snprintf(buffer + format_index, bufSize - format_index, "\n");
741  assert(format_index < bufSize);
742 
743  result += buffer;
744  memset(buffer, 0, sizeof(buffer));
745  format_index = 0;
746 
747  vector<Log2Histogram *> hist;
748  vector<string> names;
749  hist.push_back(file_system->hist_fs_lookup());
750  names.push_back("lookup");
751  hist.push_back(file_system->hist_fs_forget());
752  names.push_back("forget");
753  hist.push_back(file_system->hist_fs_forget_multi());
754  names.push_back("forget_multi");
755  hist.push_back(file_system->hist_fs_getattr());
756  names.push_back("getattr");
757  hist.push_back(file_system->hist_fs_readlink());
758  names.push_back("readlink");
759  hist.push_back(file_system->hist_fs_opendir());
760  names.push_back("opendir");
761  hist.push_back(file_system->hist_fs_releasedir());
762  names.push_back("releasedir");
763  hist.push_back(file_system->hist_fs_readdir());
764  names.push_back("readdir");
765  hist.push_back(file_system->hist_fs_open());
766  names.push_back("open");
767  hist.push_back(file_system->hist_fs_read());
768  names.push_back("read");
769  hist.push_back(file_system->hist_fs_release());
770  names.push_back("release");
771 
772  for (unsigned int j = 0; j < hist.size(); j++) {
773  Log2Histogram *h = hist[j];
774  unsigned int format_index =
775  snprintf(buffer, bufSize, "\"%s\",\"%s\",%" PRIu64 ",\"nanoseconds\"",
776  repo.c_str(), names[j].c_str(), h->N());
777  for (unsigned int i = 0; i < qs.size(); i++) {
778  format_index += snprintf(buffer + format_index, bufSize - format_index,
779  ",%u", h->GetQuantile(qs[i]));
780  }
781  format_index +=
782  snprintf(buffer + format_index, bufSize - format_index, "\n");
783  assert(format_index < bufSize);
784 
785  result += buffer;
786  memset(buffer, 0, sizeof(buffer));
787  format_index = 0;
788  }
789  return result;
790 }
791 
793  const string &socket_path,
794  MountPoint *mount_point,
795  FuseRemounter *remounter)
796  : socket_path_(socket_path)
797  , socket_fd_(-1)
798  , mount_point_(mount_point)
799  , remounter_(remounter)
800  , spawned_(false)
801 {
802  memset(&thread_talk_, 0, sizeof(thread_talk_));
803 }
804 
805 
807  if (!socket_path_.empty()) {
808  int retval = unlink(socket_path_.c_str());
809  if ((retval != 0) && (errno != ENOENT)) {
811  "Could not remove cvmfs_io socket from cache directory (%d)",
812  errno);
813  }
814  }
815 
816  if (socket_fd_ >= 0) {
817  shutdown(socket_fd_, SHUT_RDWR);
818  close(socket_fd_);
819  }
820 
821  if (spawned_) {
822  pthread_join(thread_talk_, NULL);
823  LogCvmfs(kLogTalk, kLogDebug, "talk thread stopped");
824  }
825 }
826 
827 
829  int retval = pthread_create(&thread_talk_, NULL, MainResponder, this);
830  assert(retval == 0);
831  spawned_ = true;
832 }
OptionsManager * options_mgr()
Definition: mountpoint.h:203
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
NfsMaps * nfs_maps()
Definition: mountpoint.h:200
void UnregisterQuotaListener()
Definition: cvmfs.cc:1595
int MakeSocket(const std::string &path, const int mode)
Definition: posix.cc:364
FileSystem * file_system()
Definition: mountpoint.h:422
Log2Histogram * hist_fs_opendir()
Definition: mountpoint.h:183
Statistics GetStatistics()
Definition: glue_buffer.h:517
EventList history
Definition: loader.h:180
CacheModes cache_mode()
Definition: cache_posix.h:103
virtual uint64_t GetCleanupRate(uint64_t period_s)=0
void SetHostChain(const std::string &host_list)
perf::Statistics * statistics()
Definition: mountpoint.h:438
virtual std::vector< std::string > List()=0
static const int kProbeGeo
Definition: download.h:383
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:421
virtual bool Cleanup(const uint64_t leave_size)=0
string JoinStrings(const vector< string > &strings, const string &joint)
Definition: string.cc:318
std::string PrintInodeGeneration()
Definition: cvmfs.cc:188
Cursor BeginEnumerate()
Definition: glue_buffer.h:582
const history::History * history() const
void SetProxyChain(const std::string &proxy_list, const std::string &fallback_proxy_list, const ProxySetModes set_mode)
Definition: download.cc:2430
std::string fqrn() const
Definition: mountpoint.h:420
MountPoint * mount_point_
Definition: talk.h:57
FuseRemounter * remounter_
Definition: talk.h:58
std::string FormatProxyInfo(download::DownloadManager *download_mgr)
Definition: talk.cc:123
std::string FormatLatencies(const MountPoint &mount_point, FileSystem *file_system)
Definition: talk.cc:708
uint64_t N()
Definition: algorithm.h:147
virtual std::vector< std::string > ListPinned()=0
int socket_fd_
Definition: talk.h:56
Status Check()
Definition: fuse_remount.cc:70
assert((mem||(size==0))&&"Out Of Memory")
MountPoint * mount_point_
Definition: cvmfs.cc:121
bool spawned_
Definition: talk.h:60
Log2Histogram * hist_fs_read()
Definition: mountpoint.h:187
virtual std::vector< std::string > ListCatalogs()=0
std::string loader_version
Definition: loader.h:174
virtual uint64_t GetSize()=0
string StringifyTime(const time_t seconds, const bool utc)
Definition: string.cc:105
void EndEnumerate(Cursor *cursor)
Definition: glue_buffer.h:607
void SetDnsServer(const std::string &address)
Definition: download.cc:1815
Statistics GetStatistics()
Definition: glue_buffer.h:668
virtual CacheManagerIds id()=0
virtual pid_t GetPid()=0
void SetTimeout(const unsigned seconds_proxy, const unsigned seconds_direct)
Definition: download.cc:1872
bool Pin(const string &path)
Definition: cvmfs.cc:1464
unsigned int GetQuantile(float n)
Definition: algorithm.cc:109
static TalkManager * Create(const std::string &socket_path, MountPoint *mount_point, FuseRemounter *remounter)
Definition: talk.cc:78
string * socket_path_
Definition: loader.cc:130
Type type()
Definition: mountpoint.h:205
std::string ToString()
Definition: algorithm.cc:136
string StringifyBool(const bool value)
Definition: string.cc:76
void Answer(int con_fd, const std::string &msg)
Definition: talk.cc:64
pthread_t thread_talk_
Definition: talk.h:59
static uint64_t num_instances()
Definition: shortstring.h:179
pid_t pid_
Definition: cvmfs.cc:146
static pid_t GetPid()
Definition: monitor.cc:160
Counter * Lookup(const std::string &name) const
Definition: statistics.cc:62
void SetMaxTtlMn(unsigned value_minutes)
Definition: mountpoint.cc:1721
void GetProxyInfo(std::vector< std::vector< ProxyInfo > > *proxy_chain, unsigned *current_group, unsigned *fallback_group)
Definition: download.cc:2583
catalog::ClientCatalogManager * catalog_mgr()
Definition: mountpoint.h:409
void Set(const int64_t val)
Definition: statistics.h:33
void TearDown2ReadOnly()
Definition: mountpoint.cc:1037
Log2Histogram * hist_fs_forget_multi()
Definition: mountpoint.h:180
Status CheckSynchronously()
void Flush()
Definition: tracer.cc:106
std::string Dump()
Definition: options.cc:447
Log2Histogram * hist_fs_release()
Definition: mountpoint.h:188
const loader::LoaderExports * loader_exports_
Definition: cvmfs.cc:144
TalkManager(const std::string &socket_path, MountPoint *mount_point, FuseRemounter *remounter)
Definition: talk.cc:792
virtual std::string GetStatistics()
Definition: nfs_maps.h:37
Log2Histogram * hist_fs_releasedir()
Definition: mountpoint.h:184
const char kSuffixCatalog
Definition: hash.h:52
bool NextEntry(Cursor *cursor, uint64_t *inode_parent, NameString *name)
Definition: glue_buffer.h:588
glue::NentryTracker * nentry_tracker()
Definition: mountpoint.h:434
bool IsNfsSource()
Definition: mountpoint.h:168
CacheManager * cache_mgr()
Definition: mountpoint.h:174
bool NextInode(Cursor *cursor, uint64_t *inode)
Definition: glue_buffer.h:603
Any MkFromHexPtr(const HexPtr hex, const char suffix)
Definition: hash.cc:83
bool SendFuseFd(const std::string &socket_path)
Definition: cvmfs.cc:1606
download::DownloadManager * download_mgr()
Definition: mountpoint.h:411
virtual uint64_t GetSizePinned()=0
Status ChangeRoot(const shash::Any &root_hash)
Definition: fuse_remount.cc:30
Log2Histogram * hist_fs_readlink()
Definition: mountpoint.h:182
string StringifyInt(const int64_t value)
Definition: string.cc:78
Log2Histogram * hist_fs_readdir()
Definition: mountpoint.h:185
std::string socket_path_
Definition: talk.h:55
std::string FormatHostInfo(download::DownloadManager *download_mgr)
Definition: talk.cc:99
Log2Histogram * hist_fs_open()
Definition: mountpoint.h:186
std::string GetDnsServer() const
Definition: download.cc:1807
std::string ToString() const
Definition: shortstring.h:114
QuotaManager * quota_mgr()
Definition: cache.h:198
Log2Histogram * hist_fs_lookup()
Definition: mountpoint.h:178
string ResolveProxyDescription(const string &cvmfs_proxies, const std::string &path_fallback_cache, DownloadManager *download_manager)
Definition: wpad.cc:206
uint64_t String2Uint64(const string &value)
Definition: string.cc:228
static void * MainResponder(void *data)
Definition: talk.cc:156
unsigned GetMaxTtlMn()
Definition: mountpoint.cc:1610
void ResetErrorCounters()
Definition: mountpoint.cc:533
bool Evict(const string &path)
Definition: cvmfs.cc:1451
std::string PrintList(const PrintOptions print_options)
Definition: statistics.cc:79
void GetTimeout(unsigned *seconds_proxy, unsigned *seconds_direct)
Definition: download.cc:1895
void GetReloadStatus(bool *drainout_mode, bool *maintenance_mode)
Definition: cvmfs.cc:172
static const int kProbeUnprobed
Definition: download.h:374
#define MSG_NOSIGNAL
Definition: platform_osx.h:53
Log2Histogram * hist_fs_forget()
Definition: mountpoint.h:179
~TalkManager()
Definition: talk.cc:806
static const int kProbeDown
Definition: download.h:379
static uint64_t num_overflows()
Definition: shortstring.h:180
Log2Histogram * hist_fs_getattr()
Definition: mountpoint.h:181
void GetHostInfo(std::vector< std::string > *host_chain, std::vector< int > *rtt, unsigned *current_host)
Definition: download.cc:1939
std::string PrintHierarchy() const
glue::InodeTracker * inode_tracker()
Definition: mountpoint.h:429
std::string PrintAllMemStatistics() const
static void size_t size
Definition: smalloc.h:47
virtual std::string Describe()=0
Tracer * tracer()
Definition: mountpoint.h:443
void AnswerStringList(int con_fd, const std::vector< std::string > &list)
Definition: talk.cc:69
virtual bool HasCapability(Capabilities capability)=0
download::DownloadManager * external_download_mgr()
Definition: mountpoint.h:412
void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b)
Definition: string.cc:261
void Spawn()
Definition: talk.cc:828