ROOT logo
// $Id: XrdFileCloseReporterTree.cxx 2805 2012-06-30 00:17:46Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#include "XrdFileCloseReporterTree.h"
#include "XrdFileCloseReporterTree.c7"

#include "Glasses/ZLog.h"
#include "Glasses/XrdFile.h"
#include "Glasses/XrdUser.h"
#include "Glasses/XrdServer.h"

#include "Stones/SXrdFileInfo.h"
#include "Stones/SXrdUserInfo.h"
#include "Stones/SXrdServerInfo.h"

#include "Gled/GThread.h"

#include "TFile.h"
#include "TSystem.h"
#include "TTree.h"

#include "TCint.h"
#include "TVirtualMutex.h"

// XrdFileCloseReporterTree

//______________________________________________________________________________
//
// * bFileIdxAlways: Always posfix file name with an index.

ClassImp(XrdFileCloseReporterTree);

//==============================================================================

void XrdFileCloseReporterTree::_init()
{
  mAutoSaveEntries  = 100000;
  mAutoSaveMinutes  = 60;
  mRotateMinutes    = 24 * 60;
  bRotateAtMidnight = true;

  bForceAutoSave = bForceRotate = false;

  bFileIdxAlways = true;
  mFilePrefix    = "xrd-file-access-report-";
  mTreeName      = "XrdFar";
  mFileLastIdx   = -1;
  mFile = 0;
  mTree = 0;
  mBranchF = mBranchU = mBranchS = 0;
  mXrdF = 0; mXrdU = 0; mXrdS = 0;
}

XrdFileCloseReporterTree::XrdFileCloseReporterTree(const Text_t* n, const Text_t* t) :
  XrdFileCloseReporter(n, t)
{
  _init();
}

XrdFileCloseReporterTree::~XrdFileCloseReporterTree()
{}

//==============================================================================

void XrdFileCloseReporterTree::open_file_create_tree()
{
  static const Exc_t _eh("XrdFileCloseReporterTree::open_file_create_tree ");

  TString date     = GTime::Now().ToDateLocal();
  TString basename = mFilePrefix + date;

  if (date == mFileLastDate)
  {
    ++mFileLastIdx;
    mFileNameTrue = basename + TString::Format("-%d.root", mFileLastIdx);
  }
  else
  {
    if (mFileLastDate.IsNull())
    {
      // We are just starting ... check if previous files exist.
      Int_t i = 0;
      while (true)
      {
        mFileNameTrue = basename;
        if (bFileIdxAlways || i != 0) mFileNameTrue += TString::Format("-%d", i);
        mFileNameTrue += ".root";

        if (gSystem->AccessPathName(mFileNameTrue) == true)
        {
          // No file with this name yet ... check also if a hidden file
	  // exists from a previous run (could happen if it crashed).
	  TString hfn = GledNS::pathname_make_hidden_file(mFileNameTrue);
	  if (gSystem->AccessPathName(hfn) == true)
	  {
	    break;
	  }
          else if (*mLog)
          {
            mLog->Form(ZLog::L_Warning, _eh, "Hidden file '%s' found during start-up - it will be kept as is.", hfn.Data());
          }
        }

        ++i;
      }
      mFileLastIdx = i;
    }
    else
    {
      mFileNameTrue = basename;
      if (bFileIdxAlways) mFileNameTrue += "-0";
      mFileNameTrue += ".root";

      mFileLastIdx = 0;
    }
    mFileLastDate = date;
  }

  if (*mLog)
  {
    mLog->Form(ZLog::L_Message, _eh, "Opening tree file '%s' (kept hidden until closing).", mFileNameTrue.Data());
  }

  TString fn = GledNS::pathname_make_hidden_file(mFileNameTrue);
  {
    R__LOCKGUARD2(gCINTMutex);

    mFile = TFile::Open(fn, "recreate");
    if (mFile == 0)
    {
      throw _eh + "Opening of file '" + fn + "' failed.";
    }

    mTree = new TTree(mTreeName, "Xrootd File Close Reports");
    mTree->SetAutoFlush(1000);
    mTree->SetAutoSave(0);

    mBranchF = mTree->Branch("F.", &mXrdF);
    mBranchU = mTree->Branch("U.", &mXrdU);
    mBranchS = mTree->Branch("S.", &mXrdS);
  }

  mLastAutoSave  = GTime::ApproximateTime();
  bForceAutoSave = false;

  mLastFileOpen  = GTime::ApproximateTime();
  bForceRotate   = false;
}

void XrdFileCloseReporterTree::write_tree_close_file()
{
  static const Exc_t _eh("XrdFileCloseReporterTree::write_tree_close_file ");

  TString fn(mFile->GetName());

  mTree->Write();

  TNamed xx("WritingComplete", "");
  mFile->WriteTObject(&xx);

  mFile->Close();
  delete mFile;
  mFile = 0; mTree = 0;
  mBranchF = mBranchU = mBranchS = 0;
  mXrdF = 0; mXrdU = 0; mXrdS = 0;

  gSystem->Rename(fn, mFileNameTrue);

  if (*mLog)
  {
    mLog->Form(ZLog::L_Message, _eh, "Closed tree file '%s'.", mFileNameTrue.Data());
  }

  mFileNameTrue = "";
}

//------------------------------------------------------------------------------

void XrdFileCloseReporterTree::check_file_rotate()
{
  // Should be called with cancellation disabled, no lock.

  GTime at = GTime::ApproximateTime();
  if ((mRotateMinutes > 0 && at >= mLastFileOpen + GTime(60*mRotateMinutes, 0)) ||
      (bRotateAtMidnight && at - mLastFileOpen > at.TimeOfTheDayLocal()) ||
      bForceRotate)
  {
    write_tree_close_file();
    open_file_create_tree();
    {
      GLensReadHolder _lck(this);
      Stamp(FID());
    }
  }
}

//==============================================================================

void XrdFileCloseReporterTree::ReportLoopInit()
{
  mFileNameTrue = "";
  mFileLastDate = "";
  mFileLastIdx  = -1;

  open_file_create_tree();
}

void XrdFileCloseReporterTree::ReportFileClosed(FileUserServer& fus)
{
  static const Exc_t _eh("XrdFileCloseReporterTree::ReportFileClosed ");

  GThread::CancelDisabler _cd;

  check_file_rotate();

  {
    GLensReadHolder _flck(fus.fFile);
    mXrdF->Assign(fus.fFile);
  }
  {
    GLensReadHolder _ulck(fus.fUser);
    mXrdU->Assign(fus.fUser);
  }
  {
    GLensReadHolder _slck(fus.fServer);
    mXrdS->Assign(fus.fServer);
  }

  mTree->Fill();

  if ((mAutoSaveEntries > 0 && mTree->GetEntries() % mAutoSaveEntries == 0) ||
      (mAutoSaveMinutes > 0 && GTime::ApproximateTime() >= mLastAutoSave + GTime(60*mAutoSaveMinutes, 0)) ||
      bForceAutoSave)
  {
    if (*mLog)
    {
      mLog->Form(ZLog::L_Info, _eh, "Auto-saving tree, N_entries=%lld.", mTree->GetEntries());
    }
    mTree->AutoSave("SaveSelf");

    {
      GLensReadHolder _lck(this);
      mLastAutoSave  = GTime::ApproximateTime();
      bForceAutoSave = false;
      Stamp(FID());
    }
  }
}

void XrdFileCloseReporterTree::ReportCondWaitTimeout()
{
  GThread::CancelDisabler _cd;

  check_file_rotate();
}

void XrdFileCloseReporterTree::ReportLoopFinalize()
{
  write_tree_close_file();
}

//==============================================================================

void XrdFileCloseReporterTree::RotateTree()
{
  static const Exc_t _eh("XrdFileCloseReporterTree::RotateTree ");

  if ( ! GThread::IsValidPtr(mReporterThread))
    throw _eh + "not running.";

  bForceRotate = true;
}

void XrdFileCloseReporterTree::AutoSaveTree()
{
  static const Exc_t _eh("XrdFileCloseReporterTree::AutoSaveTree ");

  if ( ! GThread::IsValidPtr(mReporterThread))
    throw _eh + "not running.";

  bForceAutoSave = true;
}
 XrdFileCloseReporterTree.cxx:1
 XrdFileCloseReporterTree.cxx:2
 XrdFileCloseReporterTree.cxx:3
 XrdFileCloseReporterTree.cxx:4
 XrdFileCloseReporterTree.cxx:5
 XrdFileCloseReporterTree.cxx:6
 XrdFileCloseReporterTree.cxx:7
 XrdFileCloseReporterTree.cxx:8
 XrdFileCloseReporterTree.cxx:9
 XrdFileCloseReporterTree.cxx:10
 XrdFileCloseReporterTree.cxx:11
 XrdFileCloseReporterTree.cxx:12
 XrdFileCloseReporterTree.cxx:13
 XrdFileCloseReporterTree.cxx:14
 XrdFileCloseReporterTree.cxx:15
 XrdFileCloseReporterTree.cxx:16
 XrdFileCloseReporterTree.cxx:17
 XrdFileCloseReporterTree.cxx:18
 XrdFileCloseReporterTree.cxx:19
 XrdFileCloseReporterTree.cxx:20
 XrdFileCloseReporterTree.cxx:21
 XrdFileCloseReporterTree.cxx:22
 XrdFileCloseReporterTree.cxx:23
 XrdFileCloseReporterTree.cxx:24
 XrdFileCloseReporterTree.cxx:25
 XrdFileCloseReporterTree.cxx:26
 XrdFileCloseReporterTree.cxx:27
 XrdFileCloseReporterTree.cxx:28
 XrdFileCloseReporterTree.cxx:29
 XrdFileCloseReporterTree.cxx:30
 XrdFileCloseReporterTree.cxx:31
 XrdFileCloseReporterTree.cxx:32
 XrdFileCloseReporterTree.cxx:33
 XrdFileCloseReporterTree.cxx:34
 XrdFileCloseReporterTree.cxx:35
 XrdFileCloseReporterTree.cxx:36
 XrdFileCloseReporterTree.cxx:37
 XrdFileCloseReporterTree.cxx:38
 XrdFileCloseReporterTree.cxx:39
 XrdFileCloseReporterTree.cxx:40
 XrdFileCloseReporterTree.cxx:41
 XrdFileCloseReporterTree.cxx:42
 XrdFileCloseReporterTree.cxx:43
 XrdFileCloseReporterTree.cxx:44
 XrdFileCloseReporterTree.cxx:45
 XrdFileCloseReporterTree.cxx:46
 XrdFileCloseReporterTree.cxx:47
 XrdFileCloseReporterTree.cxx:48
 XrdFileCloseReporterTree.cxx:49
 XrdFileCloseReporterTree.cxx:50
 XrdFileCloseReporterTree.cxx:51
 XrdFileCloseReporterTree.cxx:52
 XrdFileCloseReporterTree.cxx:53
 XrdFileCloseReporterTree.cxx:54
 XrdFileCloseReporterTree.cxx:55
 XrdFileCloseReporterTree.cxx:56
 XrdFileCloseReporterTree.cxx:57
 XrdFileCloseReporterTree.cxx:58
 XrdFileCloseReporterTree.cxx:59
 XrdFileCloseReporterTree.cxx:60
 XrdFileCloseReporterTree.cxx:61
 XrdFileCloseReporterTree.cxx:62
 XrdFileCloseReporterTree.cxx:63
 XrdFileCloseReporterTree.cxx:64
 XrdFileCloseReporterTree.cxx:65
 XrdFileCloseReporterTree.cxx:66
 XrdFileCloseReporterTree.cxx:67
 XrdFileCloseReporterTree.cxx:68
 XrdFileCloseReporterTree.cxx:69
 XrdFileCloseReporterTree.cxx:70
 XrdFileCloseReporterTree.cxx:71
 XrdFileCloseReporterTree.cxx:72
 XrdFileCloseReporterTree.cxx:73
 XrdFileCloseReporterTree.cxx:74
 XrdFileCloseReporterTree.cxx:75
 XrdFileCloseReporterTree.cxx:76
 XrdFileCloseReporterTree.cxx:77
 XrdFileCloseReporterTree.cxx:78
 XrdFileCloseReporterTree.cxx:79
 XrdFileCloseReporterTree.cxx:80
 XrdFileCloseReporterTree.cxx:81
 XrdFileCloseReporterTree.cxx:82
 XrdFileCloseReporterTree.cxx:83
 XrdFileCloseReporterTree.cxx:84
 XrdFileCloseReporterTree.cxx:85
 XrdFileCloseReporterTree.cxx:86
 XrdFileCloseReporterTree.cxx:87
 XrdFileCloseReporterTree.cxx:88
 XrdFileCloseReporterTree.cxx:89
 XrdFileCloseReporterTree.cxx:90
 XrdFileCloseReporterTree.cxx:91
 XrdFileCloseReporterTree.cxx:92
 XrdFileCloseReporterTree.cxx:93
 XrdFileCloseReporterTree.cxx:94
 XrdFileCloseReporterTree.cxx:95
 XrdFileCloseReporterTree.cxx:96
 XrdFileCloseReporterTree.cxx:97
 XrdFileCloseReporterTree.cxx:98
 XrdFileCloseReporterTree.cxx:99
 XrdFileCloseReporterTree.cxx:100
 XrdFileCloseReporterTree.cxx:101
 XrdFileCloseReporterTree.cxx:102
 XrdFileCloseReporterTree.cxx:103
 XrdFileCloseReporterTree.cxx:104
 XrdFileCloseReporterTree.cxx:105
 XrdFileCloseReporterTree.cxx:106
 XrdFileCloseReporterTree.cxx:107
 XrdFileCloseReporterTree.cxx:108
 XrdFileCloseReporterTree.cxx:109
 XrdFileCloseReporterTree.cxx:110
 XrdFileCloseReporterTree.cxx:111
 XrdFileCloseReporterTree.cxx:112
 XrdFileCloseReporterTree.cxx:113
 XrdFileCloseReporterTree.cxx:114
 XrdFileCloseReporterTree.cxx:115
 XrdFileCloseReporterTree.cxx:116
 XrdFileCloseReporterTree.cxx:117
 XrdFileCloseReporterTree.cxx:118
 XrdFileCloseReporterTree.cxx:119
 XrdFileCloseReporterTree.cxx:120
 XrdFileCloseReporterTree.cxx:121
 XrdFileCloseReporterTree.cxx:122
 XrdFileCloseReporterTree.cxx:123
 XrdFileCloseReporterTree.cxx:124
 XrdFileCloseReporterTree.cxx:125
 XrdFileCloseReporterTree.cxx:126
 XrdFileCloseReporterTree.cxx:127
 XrdFileCloseReporterTree.cxx:128
 XrdFileCloseReporterTree.cxx:129
 XrdFileCloseReporterTree.cxx:130
 XrdFileCloseReporterTree.cxx:131
 XrdFileCloseReporterTree.cxx:132
 XrdFileCloseReporterTree.cxx:133
 XrdFileCloseReporterTree.cxx:134
 XrdFileCloseReporterTree.cxx:135
 XrdFileCloseReporterTree.cxx:136
 XrdFileCloseReporterTree.cxx:137
 XrdFileCloseReporterTree.cxx:138
 XrdFileCloseReporterTree.cxx:139
 XrdFileCloseReporterTree.cxx:140
 XrdFileCloseReporterTree.cxx:141
 XrdFileCloseReporterTree.cxx:142
 XrdFileCloseReporterTree.cxx:143
 XrdFileCloseReporterTree.cxx:144
 XrdFileCloseReporterTree.cxx:145
 XrdFileCloseReporterTree.cxx:146
 XrdFileCloseReporterTree.cxx:147
 XrdFileCloseReporterTree.cxx:148
 XrdFileCloseReporterTree.cxx:149
 XrdFileCloseReporterTree.cxx:150
 XrdFileCloseReporterTree.cxx:151
 XrdFileCloseReporterTree.cxx:152
 XrdFileCloseReporterTree.cxx:153
 XrdFileCloseReporterTree.cxx:154
 XrdFileCloseReporterTree.cxx:155
 XrdFileCloseReporterTree.cxx:156
 XrdFileCloseReporterTree.cxx:157
 XrdFileCloseReporterTree.cxx:158
 XrdFileCloseReporterTree.cxx:159
 XrdFileCloseReporterTree.cxx:160
 XrdFileCloseReporterTree.cxx:161
 XrdFileCloseReporterTree.cxx:162
 XrdFileCloseReporterTree.cxx:163
 XrdFileCloseReporterTree.cxx:164
 XrdFileCloseReporterTree.cxx:165
 XrdFileCloseReporterTree.cxx:166
 XrdFileCloseReporterTree.cxx:167
 XrdFileCloseReporterTree.cxx:168
 XrdFileCloseReporterTree.cxx:169
 XrdFileCloseReporterTree.cxx:170
 XrdFileCloseReporterTree.cxx:171
 XrdFileCloseReporterTree.cxx:172
 XrdFileCloseReporterTree.cxx:173
 XrdFileCloseReporterTree.cxx:174
 XrdFileCloseReporterTree.cxx:175
 XrdFileCloseReporterTree.cxx:176
 XrdFileCloseReporterTree.cxx:177
 XrdFileCloseReporterTree.cxx:178
 XrdFileCloseReporterTree.cxx:179
 XrdFileCloseReporterTree.cxx:180
 XrdFileCloseReporterTree.cxx:181
 XrdFileCloseReporterTree.cxx:182
 XrdFileCloseReporterTree.cxx:183
 XrdFileCloseReporterTree.cxx:184
 XrdFileCloseReporterTree.cxx:185
 XrdFileCloseReporterTree.cxx:186
 XrdFileCloseReporterTree.cxx:187
 XrdFileCloseReporterTree.cxx:188
 XrdFileCloseReporterTree.cxx:189
 XrdFileCloseReporterTree.cxx:190
 XrdFileCloseReporterTree.cxx:191
 XrdFileCloseReporterTree.cxx:192
 XrdFileCloseReporterTree.cxx:193
 XrdFileCloseReporterTree.cxx:194
 XrdFileCloseReporterTree.cxx:195
 XrdFileCloseReporterTree.cxx:196
 XrdFileCloseReporterTree.cxx:197
 XrdFileCloseReporterTree.cxx:198
 XrdFileCloseReporterTree.cxx:199
 XrdFileCloseReporterTree.cxx:200
 XrdFileCloseReporterTree.cxx:201
 XrdFileCloseReporterTree.cxx:202
 XrdFileCloseReporterTree.cxx:203
 XrdFileCloseReporterTree.cxx:204
 XrdFileCloseReporterTree.cxx:205
 XrdFileCloseReporterTree.cxx:206
 XrdFileCloseReporterTree.cxx:207
 XrdFileCloseReporterTree.cxx:208
 XrdFileCloseReporterTree.cxx:209
 XrdFileCloseReporterTree.cxx:210
 XrdFileCloseReporterTree.cxx:211
 XrdFileCloseReporterTree.cxx:212
 XrdFileCloseReporterTree.cxx:213
 XrdFileCloseReporterTree.cxx:214
 XrdFileCloseReporterTree.cxx:215
 XrdFileCloseReporterTree.cxx:216
 XrdFileCloseReporterTree.cxx:217
 XrdFileCloseReporterTree.cxx:218
 XrdFileCloseReporterTree.cxx:219
 XrdFileCloseReporterTree.cxx:220
 XrdFileCloseReporterTree.cxx:221
 XrdFileCloseReporterTree.cxx:222
 XrdFileCloseReporterTree.cxx:223
 XrdFileCloseReporterTree.cxx:224
 XrdFileCloseReporterTree.cxx:225
 XrdFileCloseReporterTree.cxx:226
 XrdFileCloseReporterTree.cxx:227
 XrdFileCloseReporterTree.cxx:228
 XrdFileCloseReporterTree.cxx:229
 XrdFileCloseReporterTree.cxx:230
 XrdFileCloseReporterTree.cxx:231
 XrdFileCloseReporterTree.cxx:232
 XrdFileCloseReporterTree.cxx:233
 XrdFileCloseReporterTree.cxx:234
 XrdFileCloseReporterTree.cxx:235
 XrdFileCloseReporterTree.cxx:236
 XrdFileCloseReporterTree.cxx:237
 XrdFileCloseReporterTree.cxx:238
 XrdFileCloseReporterTree.cxx:239
 XrdFileCloseReporterTree.cxx:240
 XrdFileCloseReporterTree.cxx:241
 XrdFileCloseReporterTree.cxx:242
 XrdFileCloseReporterTree.cxx:243
 XrdFileCloseReporterTree.cxx:244
 XrdFileCloseReporterTree.cxx:245
 XrdFileCloseReporterTree.cxx:246
 XrdFileCloseReporterTree.cxx:247
 XrdFileCloseReporterTree.cxx:248
 XrdFileCloseReporterTree.cxx:249
 XrdFileCloseReporterTree.cxx:250
 XrdFileCloseReporterTree.cxx:251
 XrdFileCloseReporterTree.cxx:252
 XrdFileCloseReporterTree.cxx:253
 XrdFileCloseReporterTree.cxx:254
 XrdFileCloseReporterTree.cxx:255
 XrdFileCloseReporterTree.cxx:256
 XrdFileCloseReporterTree.cxx:257
 XrdFileCloseReporterTree.cxx:258
 XrdFileCloseReporterTree.cxx:259
 XrdFileCloseReporterTree.cxx:260
 XrdFileCloseReporterTree.cxx:261
 XrdFileCloseReporterTree.cxx:262
 XrdFileCloseReporterTree.cxx:263
 XrdFileCloseReporterTree.cxx:264
 XrdFileCloseReporterTree.cxx:265
 XrdFileCloseReporterTree.cxx:266
 XrdFileCloseReporterTree.cxx:267
 XrdFileCloseReporterTree.cxx:268
 XrdFileCloseReporterTree.cxx:269
 XrdFileCloseReporterTree.cxx:270
 XrdFileCloseReporterTree.cxx:271
 XrdFileCloseReporterTree.cxx:272
 XrdFileCloseReporterTree.cxx:273
 XrdFileCloseReporterTree.cxx:274
 XrdFileCloseReporterTree.cxx:275
 XrdFileCloseReporterTree.cxx:276
 XrdFileCloseReporterTree.cxx:277
 XrdFileCloseReporterTree.cxx:278
 XrdFileCloseReporterTree.cxx:279
 XrdFileCloseReporterTree.cxx:280
 XrdFileCloseReporterTree.cxx:281
 XrdFileCloseReporterTree.cxx:282
 XrdFileCloseReporterTree.cxx:283
 XrdFileCloseReporterTree.cxx:284
 XrdFileCloseReporterTree.cxx:285