@Override
  public void stopTiming() {
    super.stopTiming();
    if (!this.enabled) {
      return;
    }

    if (TimingsHistory.timedTicks % 20 == 0) {
      final Runtime runtime = Runtime.getRuntime();
      double usedMemory = runtime.totalMemory() - runtime.freeMemory();
      double freeMemory = runtime.maxMemory() - usedMemory;

      if (this.avgFreeMemory == -1) {
        this.avgFreeMemory = freeMemory;
      } else {
        this.avgFreeMemory = (this.avgFreeMemory * (59 / 60D)) + (freeMemory * (1 / 60D));
      }

      if (this.avgUsedMemory == -1) {
        this.avgUsedMemory = usedMemory;
      } else {
        this.avgUsedMemory = (this.avgUsedMemory * (59 / 60D)) + (usedMemory * (1 / 60D));
      }
    }

    long start = System.nanoTime();
    TimingsManager.tick();
    long diff = System.nanoTime() - start;

    CURRENT = Timings.timingsTickTimer;
    Timings.timingsTickTimer.addDiff(diff);
    // addDiff for timingsTickTimer incremented this, bring it back down to 1 per tick.
    this.record.curTickCount--;
    this.minuteData.curTickTotal = this.record.curTickTotal;
    this.minuteData.curTickCount = 1;
    boolean violated = isViolated();
    this.minuteData.tick(violated);
    Timings.timingsTickTimer.tick(violated);
    tick(violated);

    if (TimingsHistory.timedTicks % 1200 == 0) {
      MINUTE_REPORTS.add(new TimingsHistory.MinuteReport());
      TimingsHistory.resetTicks(false);
      this.minuteData.reset();
    }

    if (TimingsHistory.timedTicks % Timings.getHistoryInterval() == 0) {
      TimingsManager.HISTORY.add(new TimingsHistory());
      TimingsManager.resetTimings();
    }
  }
示例#2
0
  TimingHistory() {
    this.endTime = System.currentTimeMillis() / 1000;
    this.startTime = TimingsManager.historyStart / 1000;
    if (timedTicks % 1200 != 0 || MINUTE_REPORTS.isEmpty()) {
      this.minuteReports = MINUTE_REPORTS.toArray(new MinuteReport[MINUTE_REPORTS.size() + 1]);
      this.minuteReports[this.minuteReports.length - 1] = new MinuteReport();
    } else {
      this.minuteReports = MINUTE_REPORTS.toArray(new MinuteReport[MINUTE_REPORTS.size()]);
    }
    long ticks = 0;
    for (MinuteReport mp : this.minuteReports) {
      ticks += mp.ticksRecord.timed;
    }
    this.totalTicks = ticks;
    this.totalTime = FULL_SERVER_TICK.record.totalTime;
    this.entries = new TimingHistoryEntry[TimingsManager.HANDLERS.size()];

    int i = 0;
    for (TimingHandler handler : TimingsManager.HANDLERS) {
      this.entries[i++] = new TimingHistoryEntry(handler);
    }

    final Map<EntityType, Counter> entityCounts =
        MRUMapCache.of(LoadingMap.of(Maps.newHashMap(), Counter.loader()));
    final Map<BlockType, Counter> tileEntityCounts =
        MRUMapCache.of(LoadingMap.of(Maps.newHashMap(), Counter.loader()));
    // Information about all loaded chunks/entities
    this.worlds =
        JSONUtil.mapArrayToObject(
            SpongeImpl.getGame().getServer().getWorlds(),
            (world) -> {
              return JSONUtil.singleObjectPair(
                  String.valueOf(worldMap.get(world.getName())),
                  JSONUtil.mapArray(
                      world.getLoadedChunks(),
                      (chunk) -> {
                        entityCounts.clear();
                        tileEntityCounts.clear();

                        for (Entity entity : chunk.getEntities()) {
                          if (entity.getType() == null) {
                            SpongeImpl.getLogger().error("Entity is not registered {}", entity);
                            continue;
                          }
                          entityCounts.get(entity.getType()).increment();
                        }

                        for (TileEntity tileEntity : chunk.getTileEntities()) {
                          tileEntityCounts.get(tileEntity.getBlock().getType()).increment();
                        }

                        if (tileEntityCounts.isEmpty() && entityCounts.isEmpty()) {
                          return null;
                        }
                        return JSONUtil.arrayOf(
                            chunk.getPosition().getX(),
                            chunk.getPosition().getZ(),
                            JSONUtil.mapArrayToObject(
                                entityCounts.entrySet(),
                                (entry) -> {
                                  if (entry.getKey() == EntityTypes.UNKNOWN) {
                                    return null;
                                  }
                                  this.entityTypeSet.add(entry.getKey());
                                  return JSONUtil.singleObjectPair(
                                      ((SpongeEntityType) entry.getKey()).entityTypeId,
                                      entry.getValue().count());
                                }),
                            JSONUtil.mapArrayToObject(
                                tileEntityCounts.entrySet(),
                                (entry) -> {
                                  this.blockTypeSet.add(entry.getKey());
                                  return JSONUtil.singleObjectPair(
                                      Block.getIdFromBlock((Block) entry.getKey()),
                                      entry.getValue().count());
                                }));
                      }));
            });
  }