| 1 | package net.minecraft.profiler;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.lang.management.ManagementFactory;
|
| 5 | import java.lang.management.RuntimeMXBean;
|
| 6 | import java.net.MalformedURLException;
|
| 7 | import java.net.URL;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Timer;
|
| 11 | import java.util.TimerTask;
|
| 12 | import java.util.UUID;
|
| 13 | import java.util.Map.Entry;
|
| 14 | import net.minecraft.util.HttpUtil;
|
| 15 |
|
| 16 | public class PlayerUsageSnooper
|
| 17 | {
|
| 18 | private final Map<String, Object> snooperStats = Maps.<String, Object>newHashMap();
|
| 19 | private final Map<String, Object> clientStats = Maps.<String, Object>newHashMap();
|
| 20 | private final String uniqueID = UUID.randomUUID().toString();
|
| 21 |
|
| 22 | /** URL of the server to send the report to */
|
| 23 | private final URL serverUrl;
|
| 24 | private final IPlayerUsage playerStatsCollector;
|
| 25 |
|
| 26 | /** set to fire the snooperThread every 15 mins */
|
| 27 | private final Timer threadTrigger = new Timer("Snooper Timer", true);
|
| 28 | private final Object syncLock = new Object();
|
| 29 | private final long minecraftStartTimeMilis;
|
| 30 | private boolean isRunning;
|
| 31 |
|
| 32 | /** incremented on every getSelfCounterFor */
|
| 33 | private int selfCounter;
|
| 34 |
|
| 35 | public PlayerUsageSnooper(String side, IPlayerUsage playerStatCollector, long startTime)
|
| 36 | {
|
| 37 | try
|
| 38 | {
|
| 39 | this.serverUrl = new URL("http://snoop.minecraft.net/" + side + "?version=" + 2);
|
| 40 | }
|
| 41 | catch (MalformedURLException var6)
|
| 42 | {
|
| 43 | throw new IllegalArgumentException();
|
| 44 | }
|
| 45 |
|
| 46 | this.playerStatsCollector = playerStatCollector;
|
| 47 | this.minecraftStartTimeMilis = startTime;
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Note issuing start multiple times is not an error.
|
| 52 | */
|
| 53 | public void startSnooper()
|
| 54 | {
|
| 55 | if (!this.isRunning)
|
| 56 | {
|
| 57 | this.isRunning = true;
|
| 58 | this.addOSData();
|
| 59 | this.threadTrigger.schedule(new TimerTask()
|
| 60 | {
|
| 61 | public void run()
|
| 62 | {
|
| 63 | if (PlayerUsageSnooper.this.playerStatsCollector.isSnooperEnabled())
|
| 64 | {
|
| 65 | Map<String, Object> map;
|
| 66 |
|
| 67 | synchronized (PlayerUsageSnooper.this.syncLock)
|
| 68 | {
|
| 69 | map = Maps.<String, Object>newHashMap(PlayerUsageSnooper.this.clientStats);
|
| 70 |
|
| 71 | if (PlayerUsageSnooper.this.selfCounter == 0)
|
| 72 | {
|
| 73 | map.putAll(PlayerUsageSnooper.this.snooperStats);
|
| 74 | }
|
| 75 |
|
| 76 | map.put("snooper_count", Integer.valueOf(PlayerUsageSnooper.this.selfCounter++));
|
| 77 | map.put("snooper_token", PlayerUsageSnooper.this.uniqueID);
|
| 78 | }
|
| 79 |
|
| 80 | HttpUtil.postMap(PlayerUsageSnooper.this.serverUrl, map, true);
|
| 81 | }
|
| 82 | }
|
| 83 | }, 0L, 900000L);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | /**
|
| 88 | * Add OS data into the snooper
|
| 89 | */
|
| 90 | private void addOSData()
|
| 91 | {
|
| 92 | this.addJvmArgsToSnooper();
|
| 93 | this.addClientStat("snooper_token", this.uniqueID);
|
| 94 | this.addStatToSnooper("snooper_token", this.uniqueID);
|
| 95 | this.addStatToSnooper("os_name", System.getProperty("os.name"));
|
| 96 | this.addStatToSnooper("os_version", System.getProperty("os.version"));
|
| 97 | this.addStatToSnooper("os_architecture", System.getProperty("os.arch"));
|
| 98 | this.addStatToSnooper("java_version", System.getProperty("java.version"));
|
| 99 | this.addClientStat("version", "1.8.9");
|
| 100 | this.playerStatsCollector.addServerTypeToSnooper(this);
|
| 101 | }
|
| 102 |
|
| 103 | private void addJvmArgsToSnooper()
|
| 104 | {
|
| 105 | RuntimeMXBean runtimemxbean = ManagementFactory.getRuntimeMXBean();
|
| 106 | List<String> list = runtimemxbean.getInputArguments();
|
| 107 | int i = 0;
|
| 108 |
|
| 109 | for (String s : list)
|
| 110 | {
|
| 111 | if (s.startsWith("-X"))
|
| 112 | {
|
| 113 | this.addClientStat("jvm_arg[" + i++ + "]", s);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | this.addClientStat("jvm_args", Integer.valueOf(i));
|
| 118 | }
|
| 119 |
|
| 120 | public void addMemoryStatsToSnooper()
|
| 121 | {
|
| 122 | this.addStatToSnooper("memory_total", Long.valueOf(Runtime.getRuntime().totalMemory()));
|
| 123 | this.addStatToSnooper("memory_max", Long.valueOf(Runtime.getRuntime().maxMemory()));
|
| 124 | this.addStatToSnooper("memory_free", Long.valueOf(Runtime.getRuntime().freeMemory()));
|
| 125 | this.addStatToSnooper("cpu_cores", Integer.valueOf(Runtime.getRuntime().availableProcessors()));
|
| 126 | this.playerStatsCollector.addServerStatsToSnooper(this);
|
| 127 | }
|
| 128 |
|
| 129 | public void addClientStat(String statName, Object statValue)
|
| 130 | {
|
| 131 | synchronized (this.syncLock)
|
| 132 | {
|
| 133 | this.clientStats.put(statName, statValue);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public void addStatToSnooper(String statName, Object statValue)
|
| 138 | {
|
| 139 | synchronized (this.syncLock)
|
| 140 | {
|
| 141 | this.snooperStats.put(statName, statValue);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | public Map<String, String> getCurrentStats()
|
| 146 | {
|
| 147 | Map<String, String> map = Maps.<String, String>newLinkedHashMap();
|
| 148 |
|
| 149 | synchronized (this.syncLock)
|
| 150 | {
|
| 151 | this.addMemoryStatsToSnooper();
|
| 152 |
|
| 153 | for (Entry<String, Object> entry : this.snooperStats.entrySet())
|
| 154 | {
|
| 155 | map.put(entry.getKey(), entry.getValue().toString());
|
| 156 | }
|
| 157 |
|
| 158 | for (Entry<String, Object> entry1 : this.clientStats.entrySet())
|
| 159 | {
|
| 160 | map.put(entry1.getKey(), entry1.getValue().toString());
|
| 161 | }
|
| 162 |
|
| 163 | return map;
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | public boolean isSnooperRunning()
|
| 168 | {
|
| 169 | return this.isRunning;
|
| 170 | }
|
| 171 |
|
| 172 | public void stopSnooper()
|
| 173 | {
|
| 174 | this.threadTrigger.cancel();
|
| 175 | }
|
| 176 |
|
| 177 | public String getUniqueID()
|
| 178 | {
|
| 179 | return this.uniqueID;
|
| 180 | }
|
| 181 |
|
| 182 | /**
|
| 183 | * Returns the saved value of System#currentTimeMillis when the game started
|
| 184 | */
|
| 185 | public long getMinecraftStartTimeMillis()
|
| 186 | {
|
| 187 | return this.minecraftStartTimeMilis;
|
| 188 | }
|
| 189 | }
|
| 190 |
|