-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
KernelDiagnostics.java
185 lines (162 loc) · 5.99 KB
/
KernelDiagnostics.java
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
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.internal;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.neo4j.helpers.Format;
import org.neo4j.kernel.impl.factory.DatabaseInfo;
import org.neo4j.kernel.impl.store.StoreId;
import org.neo4j.kernel.impl.store.StoreType;
import org.neo4j.kernel.info.DiagnosticsPhase;
import org.neo4j.kernel.info.DiagnosticsProvider;
import org.neo4j.logging.Logger;
public abstract class KernelDiagnostics implements DiagnosticsProvider
{
public static class Versions extends KernelDiagnostics
{
private final DatabaseInfo databaseInfo;
private final StoreId storeId;
public Versions( DatabaseInfo databaseInfo, StoreId storeId )
{
this.databaseInfo = databaseInfo;
this.storeId = storeId;
}
@Override
void dump( Logger logger )
{
logger.log( "Graph Database: " + databaseInfo + " " + storeId );
logger.log( "Kernel version: " + Version.getKernelVersion() );
}
}
public static class StoreFiles extends KernelDiagnostics
{
private final File storeDir;
private static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ssZ";
private final SimpleDateFormat dateFormat;
public StoreFiles( File storeDir )
{
this.storeDir = storeDir;
TimeZone tz = TimeZone.getDefault();
dateFormat = new SimpleDateFormat( FORMAT_DATE_ISO );
dateFormat.setTimeZone( tz );
}
@Override
void dump( Logger logger )
{
logger.log( getDiskSpace( storeDir ) );
logger.log( "Storage files: (filename : modification date - size)" );
MappedFileCounter mappedCounter = new MappedFileCounter();
long totalSize = logStoreFiles( logger, " ", storeDir, mappedCounter );
logger.log( "Storage summary: " );
logger.log( " Total size of store: " + Format.bytes( totalSize ) );
logger.log( " Total size of mapped files: " + Format.bytes( mappedCounter.getSize() ) );
}
private long logStoreFiles( Logger logger, String prefix, File dir, MappedFileCounter mappedCounter )
{
if ( !dir.isDirectory() )
{
return 0;
}
File[] files = dir.listFiles();
if ( files == null )
{
logger.log( prefix + "<INACCESSIBLE>" );
return 0;
}
long total = 0;
// Sort by name
List<File> fileList = Arrays.asList( files );
Collections.sort( fileList, ( o1, o2 ) -> o1.getName().compareTo( o2.getName() ) );
for ( File file : fileList )
{
long size;
String filename = file.getName();
if ( file.isDirectory() )
{
logger.log( prefix + filename + ":" );
size = logStoreFiles( logger, prefix + " ", file, mappedCounter );
filename = "- Total";
}
else
{
size = file.length();
mappedCounter.addFile( file );
}
String fileModificationDate = getFileModificationDate( file );
String bytes = Format.bytes( size );
String fileInformation = String.format( "%s%s: %s - %s", prefix, filename, fileModificationDate, bytes );
logger.log( fileInformation );
total += size;
}
return total;
}
private String getFileModificationDate( File file )
{
Date modifiedDate = new Date( file.lastModified() );
return dateFormat.format( modifiedDate );
}
private String getDiskSpace( File storeDir )
{
long free = storeDir.getFreeSpace();
long total = storeDir.getTotalSpace();
long percentage = total != 0 ? (free * 100 / total) : 0;
return String.format( "Disk space on partition (Total / Free / Free %%): %s / %s / %s", total, free, percentage );
}
private static class MappedFileCounter
{
private long size;
public void addFile( File file )
{
if ( StoreType.canBeManagedByPageCache( file.getName() ) )
{
size += file.length();
}
}
public long getSize()
{
return size;
}
}
}
@Override
public String getDiagnosticsIdentifier()
{
return getClass().getDeclaringClass().getSimpleName() + ":" + getClass().getSimpleName();
}
@Override
public void acceptDiagnosticsVisitor( Object visitor )
{
// nothing visits ConfigurationLogging
}
@Override
public void dump( DiagnosticsPhase phase, Logger log )
{
if ( phase.isInitialization() || phase.isExplicitlyRequested() )
{
dump( log );
}
}
abstract void dump( Logger logger );
}